Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Node.js Building a Simple Application

Node.js Building a Simple Application

Building a simple application in Node.js typically includes the following steps:

Next, we will introduce step by step how to build a simple HTTP application with Node.js.

1. Install Node.js

First, make sure that Node.js and npm are installed on your system; you can verify this by entering the following command in the command line:



node -v

npm -v

If not yet installed, please refer to:Node.js Installation and Configuration。

2. Create the project folder

In the terminal, create a project folder my-first-node-app and enter it:

mkdir my-first-node-app

cd my-first-node-app

3. Initialize the project

Generate a package.json file through the npm init command; it contains the configuration information of the project.

Run the following command and fill in the information as prompted (you can press Enter to skip):



npm init -y

Modify the package.json file to the following content:

{

  "name": "my-first-node-app",

  "version": "1.0.0",

  "main": "app.js",

  "scripts": {

    "start": "node app.js"

  },

  "dependencies": {}

}

4. Create the application entry file

In the project root directory my-first-node-app create an app.js file. This is the application's entry file, used to set up the server and handle requests.

5. Write the server code

Write the following code in the app.js file to create a simple HTTP server:

Example

const

http

=

require

(

'http'

)

;

// Create the server

const

server

=

http.

createServer

(

(

req

,

res

)

=>

{

 

// Set the status code and headers of the HTTP response

  res.

writeHead

(

200

,

{

   

// Set the content type to HTML and specify the charset as UTF-8 so that Chinese characters are not garbled

   

'Content-Type'

:

'text/html; charset=utf-8'

 

}

)

;

 

// Send the response body

  res.

end

(

'

Hello, World!

This is my first Node.js application.

'

)

;

}

)

;

// Listen on the port

const

PORT

=

3000

;

server.

listen

(

PORT

,

(

)

=>

{

  console.

log

(

`Server is running on http

:

//localhost:${PORT}`);

}

)

;

6. Run the server

In the project root directory, run the following command to start the server:



npm start

You will see the following output in the console:



Server is running on http://localhost:3000

7. Access the server

Open a browser and visit http://localhost:3000, and you will see the page display as follows:

indicating that the server is running successfully.

8. Add routing

To handle different request paths, you can add simple routing to the server code, for example:

Example

const

http

=

require

(

'http'

)

;

const

server

=

http.

createServer

(

(

req

,

res

)

=>

{

 

if

(

req.

url

===

'/'

)

{

    res.

writeHead

(

200

,

{

'Content-Type'

:

'text/plain'

}

)

;

    res.

end

(

'Welcome to the homepage!'

)

;

 

}

else

if

(

req.

url

===

'/about'

)

{

    res.

writeHead

(

200

,

{

'Content-Type'

:

'text/plain'

}

)

;

    res.

end

(

'This is the about page.'

)

;

 

}

else

{

    res.

writeHead

(

404

,

{

'Content-Type'

:

'text/plain'

}

)

;

    res.

end

(

'404 Not Found'

)

;

 

}

}

)

;

const

PORT

=

3000

;

server.

listen

(

PORT

,

(

)

=>

{

  console.

log

(

`Server is running on http

:

//localhost:${PORT}`);

}

)

;

Through the above steps, you have built a simple Node.js application that can handle different request paths.

This basic application can be further extended, such as by adding database support or introducing the Express framework to handle complex routing, laying the foundation for more complex projects.