1. We create a project directory.
> md hello-world
2. Enter this directory and define the project configuration file.
For accurate definition, you can use the command:
D:\tmp\node\hello-world> npm info express version
npm http GET /express
npm http 200 /express
3.2.1
Now that the latest version of ExpressJS framework is 3.2.1, the configuration file is:
{
"name": "hello-world",
"description": "hello world test app",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.2.1"
}
}
3. Use npm to install the packages that the project depends on.
> npm install
Once the npm installation dependency package is completed, the subdirectory of node_modules will appear in the project root directory. The express packages required for project configuration are stored here. If the phase is verified, you can execute the command:
> npm ls
PS D:\tmp\node\hello-world> npm ls
npm WARN [email protected] No file found!
[email protected] D:\tmp\node\hello-world
└─┬ [email protected]
├── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└─┬ [email protected]
└── [email protected]
This command displays the express package and its dependencies.
4. Create an application
Now start creating the application itself. Create a file named or, depending on what you like, choose any one. Refer to express and create a new application using express():
//
var express = require('express');
var app = express();
Next, we can use app.verb() to define the route.
For example, use "GET /" to respond to the "Hello World" string, because res and req are accurate objects provided by Node, so you can call() or ('data', callback) or others.
('/', function(req, res){
var body = 'Hello World';
('Content-Type', 'text/plain');
('Content-Length', );
(body);
});
The ExpressJS framework provides higher-level methods such as (), which can save things like adding Content-Length. as follows:
('/', function(req, res){
('Hello World');
});
Now you can bind and listen to the port, call the() method, and receive the same parameters, such as:
5. Run the program
Now run the program and execute the command:
> node
Access the address with the browser: http://localhost:3000/
You can see the output result:
Hello World