Let’s start our first Node.js application: “Hello World”
Create a helloworld.js file, then enter the corresponding code in it
console.log("Hello World");
The console module provides a simple debugging console, similar to the JavaScript console provided by web browsers
The console.log command is similar to print output
Save the file and execute it with Node.js:
Then enter the following on the command line
node helloworld.js
If everything works normally, Hello World will be output in the terminal.
A basic HTTP server
First, we create a simple http application
Here we create a server.js file, then enter the following code
var http = require("http");//Import the HTTP module
http.createServer(function(request, response) {//Create the application
response.writeHead(200, {"Content-Type": "text/plain"});//Set a header so it displays properly in HTML
response.write("Hello World");//Send a response to the client
response.end();//End the response
}).listen(8888);//Then listen on port 888
Run your script with Node.js
node server.js
Next, open your browser and visit http://localhost:8888/, and you will see a webpage displaying “Hello World”.
Summary: NODEJS listens natively from the server side; from the beginning to the middle data request and finally ending the listening process, the whole process must be complete!
Server-side module importing
Import other modules used by the application; here we use the http module in server.js
Now create a server.js file
var http = require("http");//Import the http module and start the process
function start() {//Use a function for importing
function onRequest(request, response) {//The onRequest function, request and response
console.log("Request received.");//Console log
response.writeHead(200, {"Content-Type": "text/plain"});//HTML display module
response.write("Hello World");//Response displayed in the browser
response.end();//End the process
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;//Export the contents of the start function
Import the server.js module in index.js
var server = require("./server");//Import the ./server file (by default, .js can be omitted)
server.start();// server calls the start function in index.js
Run index.js
node index.js
Server has started
The browser will display
Hello World
Including Files
The Node.js file system module allows files to be included on the server side
To include the file module, use the require() method:
var fs = require('fs');
The file module can read, create, upload, delete, and rename files
The fs.readFile() method can be used to read files
Next, create a demo.html file
<html>
<body>
<h1>My Header</h1>
My paragraph.
</body>
</html>
Then create a server.js file to read the demo.html file
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('demo.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
}).listen(8888);
After saving the file, run server.js
node server.js
Now open localhost:8888 in your browser, and it will display the contents of demo.html
Create Files
File module
fs.appendFile()fs.open()fs.writeFile()
appendfile() FS。The method appends the specified content to a file. If the file does not exist, the file will be created:
var fs = require('fs');
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
open() FSThe method takes a “flag” as the second parameter. If the flag is “w” for “writing”, the specified file is opened for writing. If the file does not exist, an empty file will be created:
var fs = require('fs');
fs.open('mynewfile2.txt','w',function(err,file) {
if(err)throwerr;
console.log('Saved!');
});
writefile() FS。The method checks whether the specified file content exists. If the file does not exist, a new file containing the specified content will be created:
var fs = require('fs');
fs.writeFile('mynewfile3.txt','Hello content!',function(err) {
if(err)throwerr;
console.log('Saved!');
});
Update/Upload File
fs.appendFile()fs.writeFile()
fs.appendFile() adds specific content to the end of the file
var fs = require('fs');
fs.appendFile('mynewfile1.txt',' This is my text.',function(err) {
if(err)throwerr;
console.log('Updated!');
});
fs.writeFile() replaces the specified content
var fs = require('fs');
fs.writeFile('mynewfile3.txt','This is my text',function(err) {
if(err)throwerr;
console.log('Replaced!');
});
Delete/Rename File
fs.unlink() deletes the specified file
var fs = require('fs');
fs.unlink('mynewfile2.txt',function(err) {
if(err)throwerr;
console.log('File deleted!');
});
fs.rename() renames a file
var fs = require('fs');
fs.rename('mynewfile1.txt','myrenamedfile.txt',function(err) {
if(err)throwerr;
console.log('File Renamed!');
});