How to build nodejs LAN server

PHPz
Release: 2023-04-26 09:53:50
Original
1430 people have browsed it

Node.js is an efficient Javascript running environment that can be used to write web applications and server-side applications. It is a cross-platform technology. By building a LAN server, you can store data on your own computer for easy management and use. The following will introduce how to use Node.js to build a LAN server.

1. Install Node.js

Before you start building the server, you need to install Node.js first. Download the installation package for the corresponding system on the Node.js official website (https://nodejs.org/), and then install it.

After the installation is completed, you can enter the following statement through the command line to check whether the installation is successful:

node -v
Copy after login

If the installation is successful, the corresponding Node.js version number will be displayed.

2. Create a server

Create a new folder on the local disk to store server-related files. Then create a new js file in the folder, such as server.js. Enter the following code in the server.js file:

var http = require('http');

http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World!\n');
}).listen(8080);

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

The above code creates an HTTP server and listens to the local port 8080. Open http://localhost:8080/ in the browser and you will see the words "Hello World!".

3. Test the server

Open the command line tool, enter the folder where the server-related files are stored, and enter the following statement to start the server:

node server.js
Copy after login

You can see the console There are words "Server running at http://localhost:8080/" on it, indicating that the server has been started successfully. Then enter the local IP address and 8080 port number (for example, 192.168.0.100:8080) in the browser to test access to the server within the LAN.

4. Add functions

The above code is just a simple server and has no practical application value. Other functions can be implemented by introducing other modules and adding corresponding code.

For example, using the File System module that comes with Node.js, you can read and write local files. Modify the above server.js file and add the following code:

var fs = require('fs');

http.createServer(function(req, res) {
  fs.readFile('index.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
}).listen(8080);
Copy after login

The above code adds a response function on the server, reads the local index.html file and displays it in the browser.

You can introduce corresponding modules and write corresponding codes according to actual needs to achieve different server-side functions.

5. Summary

Through the above steps, use Node.js to build a LAN server and add corresponding functions, which can easily store and use data locally. It should be noted that after the server is set up, corresponding security measures should be set up to prevent unauthorized access. I hope this article will help you build a server using Node.js.

The above is the detailed content of How to build nodejs LAN server. 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