-
App engine (ext, callback)
The given template engine registers the callback as ext.
By default, Express dynamically uses the parsing engine (which can be viewed as a function) based on the file extension. For example, if you try to render a “foo.pug” file, Express calls the following steps internally
app.engine('pug', require('pug').__express);
Copy the code
You need this setting for engines that do not provide.__express out of the box, or if you want to use a template engine with a different name for the extension (such as the EJS engine for HTML).
app.engine('html', require('ejs').renderFile);
Copy the code
In this case, EJS provides a.renderFile() method with the same signature (path, options, callback) as Express expects, but note that it aliases the method inside ejs.__express, Therefore, if you use the “.ejs “extension, you do not need to do anything.
app.set('views', path.join(__dirname, 'views')); // Set the directory of views, the template file in app.engine('html', require('ejs').__express); // Use ejS render to render HTML, specify the HTML suffix to use the handler app.set()'view engine'.'html'); // Configure the default extension name.Copy the code
-
app.listen(path, callback)
-
app.listen(port, hostname, backlog, callback)
var express = require('express');
var app = express();
app.listen(3000);
Copy the code
What Express () returns to the app is actually a function designed to handle the request. You can easily provide HTTP and HTTPS versions using the same code order:
var express = require('express');
var https = require('https');
var http = require('http');
var app = express();
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
Copy the code
The app mount port method actually looks like this:
app.listen = function() {
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
Copy the code
-
app.param(name, callback)
Name is the parameter name or an array of these names, and callback is the callback function. This thing is just a listener, waiting to be triggered.
The parameters of the callback function are request object, response object, next middleware, parameter value, and parameter name key.
If name is an array, callback registers listeners sequentially for each parameter name.
In addition, next calls the callback function of the next parameter within this callback function, except for the last parameter. For the last parameter, calling next invokes the next middleware.
For example, when :user exists in the path path, you can map the user-loading logic to automatically supply req.user to the path, or perform validation on parameter inputs.
GET https://user/lrh
app.param('user'.function(req, res, next, id) {
// id == 'lrh'
User.find(id, function(err, user) {
if (err) {
next(err);
} else if(user) { req.user = user; // If the corresponding information is found, it is put in req.user and passed down, which will be saved for this request. next(); }else {
next(new Error('failed to load user')); }}); }); app.get('/user/:user'.function (req, res, next) {
console.log('This is going to be matched.');
next();
});
Copy the code
They will be invoked only once in the request-response loop, even if the parameters match across multiple routes
app.param('id'.function (req, res, next, id) {
console.log('CALLED ONLY ONCE');
next();
});
app.get('/user/:id'.function(req, res, next) {// Route 1 console.log('although this matches');
next();
});
app.get('/user/:id'.function(req, res) {// Route 2 console.log('and this matches too'); res.end(); }); // Print the following CALLED ONLY ONCE although this matches and this matches tooCopy the code
We are looking at an example of an array parameter
app.param(['id'.'page'].function(req, res, next, value) {// Trigger id, trigger page, next move to first get console.log('CALLED ONLY ONCE with', value);
next();
});
app.get('/user/:id/:page'.function (req, res, next) {
console.log('although this matches');
next();
});
app.get('/user/:id/:page'.function (req, res) {
console.log('and this matches too'); res.end(); }); // Print CALLED ONLY ONCE with 42 CALLED ONLY ONCE with 3 although this matches and this matches tooCopy the code
-
App.path () returns the path
var app = express()
, blog = express()
, blogAdmin = express();
app.use('/blog', blog);
blog.use('/admin', blogAdmin);
console.log(app.path()); // ' '
console.log(blog.path()); // '/blog'
console.log(blogAdmin.path()); // '/blog/admin'
Copy the code
-
app.render(view, locals, callback)
The HTML file is returned via the callback function. It takes an optional argument, which is an object that contains the view’s local variables. It’s just like res.render(), except it can’t send the render view itself to the client. (The difference between the two suggestions baidu, do not explain)
Think of its app.render() as a utility function that generates the render view string. Internal res.render() is used for app.render() to render the view.
Local variable cache is reserved for enabling view caching. Set this to true if you want to cache the view during development; View caching is enabled by default in production.
app.render('email'.function(err, html){
// ...
});
app.render('email', { name: 'Tobi' }, function(err, html){
// ...
});
Copy the code
-
app.route(path)
Personally, I like this one. It’s simple. Returns an instance of a single route that you can then use to process HTTP verbs with optional middleware. Use app.route() to avoid repeating route names (hence typos).
It’s comfortable to look at examples.
var app = express();
app.route('/events')
.all(function(req, res, next) {
// runs for all HTTP verbs first
// think of it as route specific middleware!
})
.get(function(req, res, next) { res.json(...) ; }) .post(function(req, res, next) {
// maybe add a new event...
});
Copy the code
-
app.use(path, callback , callback…)
This is something that you use a lot, mount middleware, I won’t go into too much detail.