Guest User

Untitled

a guest
Jun 24th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. let express = require('express');
  2. let path = require('path');
  3. let app = express();
  4.  
  5. app.use(express.static(path.join(__dirname,'public','views')));
  6.  
  7. app.get('/',function (req,res,next) {
  8. res.render('index');
  9. });
  10.  
  11.  
  12.  
  13.  
  14.  
  15. // Route and GET or POST ........
  16. /*app.route('/')
  17. .get(function (req,res,next) {
  18. res.end('GET Hello Word');
  19. }).post(function (req,res,next) {
  20. res.end('POST Hello Word');
  21. })*/
  22. //--------------------------------------------------------------
  23. // Поледовательные опработчики url
  24. /*app.get('/',
  25. function (req,res,next) {
  26. res.write("First Hello ");
  27. next();
  28. });
  29.  
  30. app.get('/',function (req,res,next) {
  31. res.end("Second World ");
  32.  
  33. })*/
  34. //----------------------------------------------------------
  35. //locals хранить настройки которые будут доступны везде
  36. /*app.locals.mysettings = 'Set1';
  37.  
  38. app.get('/',function (req,res,next) {
  39. console.log(app.locals.mysettings);
  40. res.write("Second World ")
  41. res.end("Second World dfaasd")
  42.  
  43. })*/
  44. //---------------------------------------------------------------
  45. //---------app.set -настройка может быть включена или выключена
  46. /*app.set('mysetting','myset_1');
  47.  
  48. app.get('/',function (req,res,next) {
  49.  
  50. console.log(app.get('mysetting'));
  51. app.enable('mysetting');
  52. console.log(app.enabled('mysetting'));
  53. app.disable('mysetting');
  54. console.log(app.enabled('mysetting'));
  55.  
  56.  
  57. res.write("Second World ")
  58. res.end("Second World dfaasd")
  59.  
  60. })*/
  61. //----------------------------------------------------------------------
  62. //---------Проходной обработчик---------------------
  63. /*
  64. // /user/!***
  65. app.use('/user',function (req,res,next) {
  66. //same cod
  67. console.log("First middleware");
  68. next();
  69. });
  70.  
  71. app.use(function (req,res,next) {
  72. //same cod
  73. console.log("Second middleware");
  74. next();
  75. });
  76. */
  77. //---------доступ к филдам Request------------
  78. /*app.get('/',function (req,res,next) {
  79. console.log("Проверка на Свежесть запроса "+req.fresh);
  80. console.log("Проверка на старость запроса "+req.stale);
  81. console.log("Достать нейм хоста "+req.hostname);
  82. console.log("Вывод IP "+req.ip);
  83. console.log("Вывод метода "+req.method);
  84. console.log("Вывод URL "+req.originalUrl);
  85. console.log("Вывод Protocol "+req.protocol);
  86. console.log("Вывод Protect "+req.secure);
  87. console.log("Вывод AJax "+req.xhr);
  88. console.log("Вывод acceptsLanguage "+req.acceptsLanguage('uk'));
  89. console.log("Вывод about browser "+req.get('user-agent'));
  90.  
  91.  
  92. res.send("End World")
  93. });*/
  94.  
  95. //---------------доступ к филдам Responce------------
  96. /*app.get('/',function (req,res,next) {
  97.  
  98. console.log("Проверка before "+res.headersSent);
  99. res.write('fdffd');
  100. console.log("Проверка after "+res.headersSent);
  101. res.end();
  102.  
  103. });*/
  104. //---------------доступ к филдам Responce----locals--------
  105. /*app.get('/',function (req,res,next) {
  106. res.locals.myVariable = 'I have Power';
  107. next();
  108. });
  109.  
  110. app.get('/',function (req,res,next) {
  111. console.log(res.locals.myVariable);
  112. res.end();
  113. });
  114. app.get('/',function (req,res,next) {
  115. console.log(res.locals.myVariable);
  116. res.end();
  117. });*/
  118. //---------------доступ к филдам Responce----Headers--------
  119. /*app.get('/',function (req,res,next) {
  120. // console.log(res.locals.myVariable);
  121. res.append('Content-type','text/plain'); //---or -->
  122. /!* res.set({
  123. 'asd': 'hdfjjfdffj',
  124. 'asd': 'hdfjjfdffj'
  125. });*!/
  126.  
  127. res.format({ //chek by postman
  128. 'text/plain': function () {
  129. res.send('first plain')
  130. },
  131. 'text/html': function () {
  132. res.send('second html')
  133. }
  134. })
  135.  
  136. });*/
  137. //---------------доступ к филдам Responce---------send status to browser
  138. /*
  139. app.get('/',function (req,res,next) {
  140. res.sendStatus(404); //send status to browser
  141.  
  142. });
  143. */
  144.  
  145. app.listen(3000,(err)=>{
  146. if (err)
  147. console.log(err)
  148. else
  149. console.log("Server runing");
  150. })
Add Comment
Please, Sign In to add comment