Home >Web Front-end >Front-end Q&A >How to build a simple web server using Node.js

How to build a simple web server using Node.js

PHPz
PHPzOriginal
2023-04-26 09:14:541155browse

Node.js is a JavaScript runtime environment for back-end services, which allows developers to write server-side and client-side applications in the same language. Node.js has higher processing power and better scalability compared to other backend technologies. In this article, we will introduce how to build a simple but powerful web server using Node.js.

Step One - Install Node.js

First, you need to install Node.js on your computer. You can download the installation package from the Node.js official website and install it. Once Node.js is successfully installed, you can run node -v from the command line to verify that it is working properly.

Step 2 - Install dependencies

Before we start writing the application, we need to install some necessary dependencies. Go into your project folder on the command line and run the following command:

npm init -y

This will create a package.json file that stores information about our application and All dependencies.

Next, we need to install express and nodemon. express is a popular web framework that can help us quickly create routing and middleware. nodemon is a development tool that automatically restarts the server when we save code.

Instal these dependencies by entering the following command at the command line:

npm install express nodemon --save

The --save option adds the dependencies to our package. json file so that we can easily rebuild the application later.

Step 3 - Create Server

Now let’s write our first server. Please create a file named index.js in the project folder and enter the following code:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
res.send('Hello World!');
});

app.listen( 3000, () => {
console.log('Server listening on port 3000');
});

Here, we introduced the express library and created a file named instance of app. We add a GET route here that will respond against the root URL and will Hello World! Sent back to client.

Finally, we use app.listen() to bind the server to a specific port. In this example, we have bound our server to port 3000.

Step 4 - Start the Server

Now that we have written our first server, we can use nodemon to start it. Enter the following command at the command line:

nodemon index.js

This will launch our application and automatically restart the server when the code changes. Now, go to http://localhost:3000 in your browser and you should be able to see Hello World! information.

Step Five - Add More Routes

Now that we have created our first server, let's add more routes and send more data to the client. Add the following code to the index.js file:

app.get('/users', (req, res) => {
const users = [

{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },

];

res.json(users);
});

In this example, we create a new route that responds to the /users URL and adds a A JSON object containing both users is sent back to the client.

Now visit http://localhost:3000/users, you will see the following JSON response:

[
{

"id": 1,
"name": "John"

},
{

"id": 2,
"name": "Jane"

}
]

We can also encapsulate the routes in separate files to better organize the code.

Step 6 - Use static files

If we want the server to provide static files, such as images, style sheets and JavaScript files, we can use the built-in middleware function provided by express. Add the following code to the index.js file:

app.use(express.static('public'));

Here, we tell express to use the public folder to provide static files. We can create a file called style.css in the public folder and link to it in the HTML file.

Through the above steps, we have learned to use Node.js and express to build a simple but powerful web server. Hopefully this article will help you get started quickly and start building your own web applications.

The above is the detailed content of How to build a simple web server using Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn