SoFunction
Updated on 2025-03-10

ExpressJS Getting Started Example

1. We create a project directory.

Copy the codeThe code is as follows:

> md hello-world

2. Enter this directory and define the project configuration file.
For accurate definition, you can use the command:
Copy the codeThe code is as follows:

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:
Copy the codeThe code is as follows:


    "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.

Copy the codeThe code is as follows:

> 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:
Copy the codeThe code is as follows:

> 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():

Copy the codeThe code is as follows:

//  
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.

Copy the codeThe code is as follows:

('/', 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:

Copy the codeThe code is 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:

Copy the codeThe code is as follows:

> node

Access the address with the browser: http://localhost:3000/
You can see the output result:
Copy the codeThe code is as follows:

Hello World