How to use Node.js to build a server and implement HTML5 pages

PHPz
Release: 2023-04-17 16:06:33
Original
1416 people have browsed it

With the booming development of the mobile Internet, more and more web developers are beginning to shift from PC websites to mobile web application development. In this exciting era, Node.js has become one of the essential skills for front-end developers. I believe many beginners want to learn how to use Node.js to build a server and implement a simple web application. So today let us learn how to use Node.js to build a server and implement a basic HTML5 page.

1. What is Node.js

Node.js is a JavaScript runtime environment based on the Chrome V8 engine, which can run in a server environment outside of the browser. The emergence of Node.js allows front-end developers to directly use JavaScript for back-end development without having to learn other back-end languages ​​and frameworks. This provides more possibilities for front-end development.

Features of Node.js:

  1. Asynchronous I/O: Node.js adopts an event-driven, asynchronous I/O programming model, which can efficiently handle a large number of concurrent connections, while Save system resources.
  2. Single-threaded: Node.js has only one thread, but through event polling and asynchronous I/O, it can handle multiple connections without blocking other tasks.
  3. Suitable for real-time applications: Node.js is suitable for real-time applications, such as chat rooms, online games, collaborative programming, etc. It can enable messages to be transmitted in real-time between the client and the server, improving the real-time interaction experience. Also supports continuous connections.

2. Environment setup

  1. Install Node.js

Download the appropriate version from the official website https://nodejs.org/en/ Get the Node.js installation package on your computer and follow the prompts to install it.

  1. Create project

Open the console, create a new folder and enter:

mkdir nodejs-server
cd nodejs-server
Copy after login

Then create a server file in the directory and name it server .js:

touch server.js
Copy after login
  1. Write code

Open the server.js file and enter the following code:

var http = require('http');  //引入http模块
var fs = require('fs');  //引入fs模块

var server = http.createServer(function(req, res){
  console.log("Request received from "+ req.url);  //打印请求的路径

  res.writeHead(200, {'Content-Type': 'text/html'});   //设置响应头部,200表示一切正常

  var html = fs.readFileSync(__dirname + '/index.html', 'utf-8');  //读取html文件,__dirname表示当前文件所在的目录路径
  res.end(html);   //将读取的内容返回
});
server.listen(3000);   //监听3000端口
console.log("Server running at http://localhost:3000/");
Copy after login

Explain the code:

  1. First introduce the http and fs modules of Node.js.
  2. Create a server object through the http.createServer() method.
  3. In the callback function of the server object, the requested path is printed on the console.
  4. The response header is set, the index.html file is read, and its content is returned.
  5. In the last line, port 3000 is listened to.
  6. Create HTML file

Create an index.html file under the nodejs-server folder for the HTML page returned to the client.

Enter the following code:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Node.js Server</title>
  </head>
  <body>
    <h1>Node.js Server</h1>
    <p>Welcome to my Node.js Server!</p>
  </body>
</html>
Copy after login

3. Run the project

Execute the following command on the console:

node server.js
Copy after login

Then enter http:/ in the browser /localhost:3000/, you can see a simple HTML page.

4. Summary

This article briefly introduces how to use Node.js to build a simple server and achieve basic HTML page access. Therefore, it can be understood that Node.js has good scalability and can easily implement basic operations such as data processing, observation and service environment control in web applications, jointly providing support for the creation of efficient web application platforms, and it can also be used as A more convenient choice for front-end developers.

The above is the detailed content of How to use Node.js to build a server and implement HTML5 pages. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!