Open the first node. JS Express Project

Source: Internet
Author: User

Manually creating an Express.js app can be broadly divided into the following steps:

1. Create a folder

A. Create a project root folder, such as Helloword

B. Create a directory structure for the project at the root of the project, creating {Public,public/css,public/img,public/js,db,views,views/includes,routes} in turn

You can use the command:

mkdir  {public,public/css,public/img,public/js,db,views,views/includes,routes}

2.NPM initialization and configuration of Package.json

A. Enter the console and enter the root directory of the project

B. Input command: NPM init, then enter some relevant parameters, name,version, etc. can refer to the following Package.json file

C. Use command: NPM install [email protected]--save

3. Declaration of dependency

Since Express.js is a contract-superior configuration, you can also enter the following directly into the Package.json, and then run the NPM install

{  "name": "HelloWorld",  "version": "0.0.1",  "description": "\" \ "" ",  " main ":" App.js ",  " scripts " : {    "start": "Node App.js"  },  "author": "",  "license": "ISC",  "dependencies": {    "express": " 4.1.2 "," Jade ":" 1.3.1 "," Mongoskin ":" 1.4.1 "," stylus ":" 0.44.0 "  }}

4.app.js Project Startup file configuration

Create the project start file app.js in the root directory, open app.js and edit

The configuration file for App.js mainly involves the following steps:

1) Introduction of dependency,

// dependency introduced var express = require (' Express '= require (' http '= require (' path ');

2) Setting the relevant configuration

Instantiation of Express.jsvar app = Express ();
Configuration port, views, views Engineapp.set (' Port ', Process.env.PORT | | |); App.set ("Views", Path.join (__dirname, ' views ')) ;//The __dirname underline here is two English underline app.set (' View engine ', ' Jade ');

3) Connect to database (optional)

4) Define Middleware

5) Defining routes

Define Route App.all ('/', function (req,res) {res.render (' index ', {msg: ' Welcome to the Practical node.js! '}); App.get ('/about ', function (req,res) {res.end (' about Us ');});

6) Create a service

Create a service Http.createserver (app). Listen (App.get (' Port '), function () {console.log (' Express.js server listening on port ' + App.get (' Port ');});

Before you start the service, you need to create a index.jade in the Views folder, with the following code:

H1 hellop= msg

7) Open Service

Typing in the command line: node app.js and entering http://localhost:3000/in the browser will get:

Entering http://localhost:3000/about in the browser will get:

5. Combining Jade

6. Run the project

Open the first node. JS Express Project

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.