Node. js note (1) Project Creation

Source: Internet
Author: User

Node. js note (1) Project Creation
1. supervisor can help you implement this function. It will monitor your code changes and automatically restart Node. js. It is easy to use. First, use npm to install supervisor: 2. app. js is the entry of the Project, which defines the route controller routes/index. js is the content of the routing controller file used to organize the display. 3. index. ejs is the template file, namely, routes/index. templates called in js 4. app. get ('/hello', routes. hello); Set routing rules for fixed paths 5. app. get ('/user/: username', function (req, res) {res. send ('user: '+ req. params. username) ;}); advanced route matching mode 6. app. set ('view', _ dirname + '/view'); app. set ('view engine ', 'ejs'); the template engine indicates that if the template engine is ejs, the template is located in the rou directory under the views subdirectory. Tes/index. js exports. the index function uses statements to call the template engine: res. render ('index', {title: 'express '}) res. render calls the template engine and returns the result to the browser client. It accepts the Template Name as the first parameter of the two parameters, the second parameter is the data to be converted in the template. There are only three tag systems used to translate ejs from the template: <% code %> indicates javascript code, <% = code %> display the contents that have been converted into special HTML characters <%-code %> display the original HEML content 7, layout. ejs is a webpage layout template. Generally, many repeated headers and footers are written in this template. You can also disable the template function in the app. js app. layout: false in configure another scenario is that a project may have multiple webpage layout templates, either on the front-end or on the backend, or a layout template can be created during template translation. Res. render ('userlist', {title: 'user list background management system', layout: 'admin '}); 8. Download Twitter Bootstrap from http://jquery.com/download the latest version of jquery from http://twitter.github.com/bootstrap. js samples // Node. js build a project ///////////////////////////////////// ///////////----------------------------------------------------------- ------------------------------ 1. install the Express framework and $ npm install-g express // install $ npm install-g express-generator globally. Do not forget this command, otherwise, the system will prompt "express command cannot be found" 2. Create the project express-e microblog // that is, ejs,-j (that is, jade) (the old command changed after Express 4.0 is released) 3. Create a dependency cd microblog // microblog to create the npm install4 folder for the previous project, start the project npm startwkiol1yp00kjuis7aac6lfv_1921368.jpg 5, close the server, and press Ctrl + C. Project entry file app. js var express = require ('express '); var path = require ('path'); var favicon = require ('serve-favicon '); var logger = require ('Morgan '); var cookieParser = require ('cookie-parser'); var bodyParser = require ('body-parser '); var routes = require ('. /routes/Index'); var users = require ('. /routes/users '); var app = express (); // view engine setupapp. set ('view', path. join (_ dirname, 'view'); app. Set ('view engine ', 'ejs'); // uncomment after placing your favicon in/public // app. use (favicon (path. join (_ dirname, 'public', 'favicon. ico '); app. use (logger ('dev'); app. use (bodyParser. json (); app. use (bodyParser. urlencoded ({extended: false}); app. use (cookieParser (); app. use (express. static (path. join (_ dirname, 'public'); app. use ('/', routes); app. use ('/users', users); // catch 404 and forward t O error handlerapp. use (function (req, res, next) {var err = new Error ('not found'); err. status = 404; next (err) ;}); // error handlers // development error handler // will print stacktraceif (app. get ('enabled') = 'development') {app. use (function (err, req, res, next) {res. status (err. status | 500); res. render ('error', {message: err. message, error: err}) ;}/// production error handler // no stackt Races leaked to userapp. use (function (err, req, res, next) {res. status (err. status | 500); res. render ('error', {message: err. message, error :{}}) ;}; module. exports = app; 6. app. set is a parameter setting tool for Express. It accepts a Key and a Value as the following available parameters: basepath: base address, usually used for res. redirect () Jump to views: view File directory, store the Template File view engine: view template engine view options: Global view parameter object view catch: Enable view cache case sensitive routes: whether the path is case sensitive or not. strict routing: strict path. "/" Jsonp callback: enables transparent jsonp support 7. Express dependency and connect. It provides a large number of middleware and is enabled using app. use. The function of bodyParser is to parse client requests, usually content transmitted through POST. 8. app. use ('/', routes); app. use ('/users', users); is a routing controller. If the request is an access "/" path, it is forwarded '. /routes/Index. 9. app. listen (3000); Start and listen to port 3000. This app. if it is not written in js, the default port is 3000 in the file "www" in the/bin/directory,/*** Get port from environment and store in Express. */var port = normalizePort (process. env. PORT | '000000'); app. set ('Port', port );... /*** Listen on provided port, on all network interfaces. */server. listen (port); 10. routes/index. js is a Routing File, equivalent to the Controller, var express = require ('express '); var router = express. router ();/* GET home page. */router. get ('/', function (Req, res, next) {res. render ('index', {title: 'express '}) ;}); module. exports = router; 11. index. ejs is a template file, that is, routes/index. the template called in js. Content:

Related Article

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.