Minute hand net-it education: Nodejs A new project and code explanation

Source: Internet
Author: User

Children 's shoes that know Nodejs know, Nodejs have a lot of dependence, then we in the new project, how to quickly import these dependencies to create a Nodejs project, the next step is described in detail. here Amway a front-end development tool--webstorm. Really good, except a little (perhaps not a little) to account for the memory, open a bit slow. This will be very handy in the work (consider not to write a comparison of the front-end development tools, Roar roar ~ (≧▽≦)/~) ? Let's use the command to create a Nodejs project.  1. We create the project under the WS folder in drive C. Open the DOS window and go to the WS folder  2. Create a project named NodeProjection1 under this folder  3. Go to the project folder, install dependencies    then select a development tool to open the project you just created. Such as. Node_modules has a lot of dependent modules, far more than you see.    ? Here is a detailed explanation of the functional uses of each directory in the project The app.js:--boot file, or the main file entry, can be said. package.json--defines the various modules required for this project, as well as the configuration information (such as name, version, license, etc.) of the project. The NPM install command automatically downloads the required modules based on this configuration file, which is the run and development environment required to configure the project.  node_modules--Store the modules installed in the Package.json, when you add the dependent modules in the Package.json and install them, store them in this folder public--store image, CSS, JS and other filesroutes--Storing routing filesviews--storing view files or template files.bin--Storing executable files the code in App.js and its explanation  1. Here we loaded the Express, path modules, and the index. JS and Users.js routing files under the Routes folder via require (). The meanings of each line of code are explained below. 2.var app = Express (); Generate an express instance. App. 3.app.set (' views ', Path.join (__dirname, ' views ')); Sets the view folder as the directory where the file is stored, that is, the directory where the template file is stored. _dirname is a global variable that stores the directory in which the currently executing script resides.  4.app.set (' View engine ', ' Ejs '); Set the view template engine to Ejs.  5.app.use (Logger (' dev ')): Loads the log middleware.  6.app.use (Bodyparser.json ()): Loads the middleware that parses the JSON.  7.app.use (bodyparser.urlencoded ({extended:false}));loads the middleware that parses the urlencoded request body.  8.app.use (Cookieparser ()): Loads the middleware that parses the cookie.  9.app.use (Express.static (Path.join (__dirname, ' Public ')): Sets the public folder to the directory where the static files are stored.  10.app.use ('/', index), and App.use ('/users ', users): Routing controller.  11.app.use (function (req, res, next) {var err = new Error (' not Found ');err.status = 404;Next (err);});The 404 error is captured and forwarded to the error handler.  12.app.use (function (err, req, res, next) {//Set locals, only providing error in developmentres.locals.message = err.message;res.locals.error = req.app.get (' env ') = = = ' Development '? ERR: {}; //Render the error pageres.status (Err.status | |);res.render (' error ');});the error handler in the development environment renders the error message and displays it in the browser.  13.module.exports = app;: Export the app instance for other modules to invoke.  ? bin/www code and its explanations  1.#!/usr/bin/env node: Indicates that it is a node executable file.  2.var debug = require (' Debug ') (' Nodeproject1:server '): Introduces the Debug module and prints the debug log.  3.var app = require ('.. /app '): Introduce the app instances we've exported above.  4.var port = normalizeport (Process.env.PORT | | ' n ');app.set (' Port ', port); Set port number.  5. The remaining code is used to start the project and listen for 3000 ports.  ? routes/index.js  module.exports = router;generate a routing instance to capture a GET request to access the home page, export the route and pass App.use ('/', routes) in App.js; This way, when the home page is accessed, Res.render (' index ', {title: ' Express '}) is called, and the Views/index.ejs template is rendered and displayed in the browser.  ? Views/index.ejs  when we render the template, we pass in a variable with the title value of the Express string, and the template engine replaces all <%= title%> with Express, and then displays the rendered HTML to the browser.   reprint:http://www.jianshu.com/p/7b0a5d4491ba Cave ListeningThis article reproduced from the minute hand netWant to learn more it knowledge can be Dabigatran: 272292492

Minute hand net-it education: Nodejs A new project and code explanation

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.