Home  >  Article  >  Web Front-end  >  How to set up a site directory in Node.js

How to set up a site directory in Node.js

PHPz
PHPzOriginal
2023-04-11 09:14:361034browse

Node.js is a JavaScript runtime environment based on the Chrome V8 engine, which allows JavaScript to run independently of the browser. More and more web developers choose to use Node.js to develop web applications, and the unified parsing and efficient operation of I/O features of Node.js make it very suitable for building lightweight, efficient management and development. Web server. This article will introduce how to set up a site directory in Node.js to help developers better manage and develop web servers.

1. Why do you need to set up a site directory?

In a web server, the site directory refers to the folder or directory where web applications are stored on the server. When a client requests access to a page, the server needs to find the file where the page is located and return it to the client. Therefore, the web server needs to know exactly the directory where the web application is located in order to quickly respond to client requests. If the site directory is not set, the server will not be able to find the application's files and the client will not be able to access the website.

2. How to set up the site directory?

The http module of Node.js is one of the most basic modules for creating a web server. Below we will use the http module to set up the site directory.

First, we need to use Node.js’ built-in module “path” to parse the file path. In Node.js, file paths can be expressed in two ways: "/" and "\". In order to avoid path syntax differences caused by different operating systems, we use the "path.normalize()" method to normalize the path.

const path = require('path');
const root = path.normalize(__dirname + '/../'); // 直接上一级目录作为根目录

Next, we need to use the http module of Node.js to create a web server. Before creating the server, we need to use the "fs" module to read static resource files and the "mime" module to set the Content-Type response header information.

const http = require('http');
const fs = require('fs');
const mime = require('mime');

Next, we use the "createServer()" method of the http module to create a web server, and use the "req.url" attribute to obtain the URL requested by the client, and use the "fs.readFile()" method to read the file contents.

http.createServer((req, res) => {
  const filename = path.join(root, req.url); // 获取请求的文件名,加上根目录得到完整的文件路径
  fs.readFile(filename, (err, data) => {
    if (err) {
      res.statusCode = 404;
      res.end('Not Found');
    } else {
      res.statusCode = 200;
      res.setHeader('Content-Type', mime.getType(filename)); // 设置Content-Type响应头
      res.end(data);
    }
  });
}).listen(8080);

Finally, we start the web server and listen on the specified port. In the above code, we have used an arrow function to handle client requests. If the requested file does not exist, we will return a response with HTTP status code 404.

console.log('Server running at http://localhost:8080/');

3. Summary

Setting up the site directory in Node.js is one of the key steps in web server development. In this article, we introduced how to use Node.js's http, path, fs, and mime modules to create a web server and set up the site directory. I hope this article can help developers better understand and apply Node.js related knowledge.

The above is the detailed content of How to set up a site directory in 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