node. JS Notes

Source: Internet
Author: User

First look at the app.js.
1.app.set (Name,value)
Set the value of the item named name to value, which sets the parameter
App.set (' Views ', Path.join (__dirname, ' views ')); The path of the template folder is set, and the main meaning of the __dirname is clear, it is a global variable in node. js that represents the path to the current execution file.
App.set (' View engine ', ' Ejs '); Set up using the template engine that we use for the Ejs
2.app.use ([path], function)
Using this method to use the middleware, because express relies on connect, there are a large number of middleware, can be used through the App.use, the path parameter can not be filled, the default is '/' (the project is not used to explain separately, when used to self-check the middleware part of the API)
App.use (Express.static (Path.join (__dirname, ' public ')); It may be noted in this sentence that express.static () handles static requests, sets the public file, and all files in public are returned as static data files (such as styles, scripts, picture footage, and so on)
var routes = require ('./routes/index '); var users = require ('./routes/users '); App.use ('/', routes); App.use ('/users ', Users);
The above code indicates that when the user uses/accesses, call routes, that is, the Index.js file under the routes directory, where the. js suffix is omitted, and the routes file is called when the Users.js directory is accessed with/users.
This is why, in our example with http://localhost:8100/access is, the modified Index.js file code can be executed (of course, the Index.js file also write the corresponding code in order to be the effect we finally see)
3.app.get (name)
Gets the value of the item named name
if (App.get (' env ') = = = ' Development ') {App.use (function (err, req, res, next) {Res.status (Err.status | |); Res.render ( ' Error ', {message:err.message, error:err}); });}
Indicates that if it is a development environment, the stack information is output when handling error
4. Routing Files Index.js
Look at the following code
Router.get ('/', function (req, res) {res.render (' index ', {title: ' This paragraph indicates that router.get indicates that the response function is processed by a GET request/time, and two parameters are request, response, respectively;
Res.render means that the template engine is called to parse the name index, passing in and passing the title and the users two objects as parameters;
Why does it know the Index.ejs in the table of contents of the solution board? Instead of files in other directories, or after other suffix names?
The reason is the setting in App.js:
App.set (' Views ', Path.join (__dirname, ' views ')); App.set (' View engine ', ' Ejs ');
And these two parameters can be used in Index.ejs, then add the Ejs part, will return the final generated page show!
Res.send ("Req.host gets the hostname, Req.path gets the request path name!");

node. JS Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.