learning express step(十一)】的更多相关文章

learning express.Router() code: const express = require('express'); const app = express(); var router = express.Router(); var cookieParser = require('cookie-parser'); app.use(cookieParser()); router.use(function (req, res, next) { //if (!req.headers[…
learning express error handle code: const express = require('express'); const app = express(); const fs = require('fs'); app.get('/', function (req, res, next){ fs.readFile('file', function (err, data) { if(err){ next(err) }else{ res.send(data); } })…
learning express error handle code: const express = require('express'); const app = express(); app.get('/', function (req, res){ throw new Error("BROKEN"); }); app.listen(); result:…
learning express view engine function const express = require('express'); const app = express(); app.set('views','./views'); app.set('view engine', 'pug'); app.get('/', function(req, res){ res.render('index',{title: 'hey', message: 'hello panzidong u…
learning  express  middleware var express = require('express'); var app = express(); var myLogger = function (req, res, next) { log++; console.log('var log =', log); next(); }; app.use(myLogger); app.get('/', function(req, res){ res.send("hello world…
learning express route function const express = require('express'); const app = express(); app.get('/', function (req, res) { res.send('root'); }); app.get('/about', function (req, res) { res.send('about'); }); app.get('/random.txt',function (req, re…
router-level middleware works in the same way as application-level middleware, except it is bound to an instance of  express.Router(). code: var express = require('express'); var app = express(); var router = express.Router(); // a middleware functio…
To skip the rest of the middleware functions from a router middleware stack, call next('route') to pass control to the next route. const express = require('express'); const app = express(); app.get('/user/:id', function (req, res, next) { ') next('ro…
Route handlers enable you to define multiple routes for a path. The example below defines two routes for GET requests to the /user/:id path. The second route will not cause any problems, but it will never get called because the first route ends the r…
code: use application middleware var express = require('express'); var app = express(); app.use(function (req, res, next) { console.log('Time', Date.now()); next() }); app.use('/user/:id', function (req, res, next) { console.log('Request Type:', req.…