This article is written by a student studying JS instead of a memo. Don't expect the content.
This time it will be done on Ubuntu, so start the virtual environment with iTerm2.
Where it started
Move to the directory where Ubuntu is installed. vagrant up is a command to start Ubuntu installed on a virtual PC, and vagrant ssh connects to SSH with the Vagrant virtual machine set.
Write the following in the directory.
echo "'use strict';" >file name
The first line is written when starting a new project with yarn.
Describe the following in the file described earlier.
const http = require('http');
const server = http.createServer((request, response) => {
response.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
response.write(request.headers['Anything is fine']);
response.end();
});
server.listen(8000, () => {
console.log('Listening on 8000' );
});
The first line is a description for using JS in strict mode.
The second line assigns the http module to the argument http.
Lines 3-9 are the server description. I am building a server using the http module assigned to the argument. See below for the description method.
http.createServer(Server-side processing)
This time, the arrow function is used in the server side processing, and the request is assigned to the first argument and the response is assigned to the second argument.
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8'
});
This code writes a response header that sets the information handled by the server, along with a status code of 200 that indicates success. The 7th line uses the write function to display the string in the request header. Depending on the content you want to display, you can also write as follows.
res.write(
'<!DOCTYPE html><html lang="ja"><body><h1>String</h1></body></html>'
);
The 8th line shows that the server has finished writing.
From the 10th line onward, the port that starts the server is set to 8000, and the listen function is used to permanently check for requests from a specific port. This time, the character string is displayed on the console as soon as there is a request.
Write the following and check the operation with REPL.
If the character string of the console is displayed, it is successful. Thank you for your hard work!!
yarn is a package manager (https://yarnpkg.com/en/) that has the same role as npm, which is automatically installed when you install node. You can install packages faster than npm by installing in parallel.
The "http module" is used to build functionality as an HTTP server or HTTP client. You can publish your website on the net and send and receive data from forms. Of course, it is possible to build not only static websites but also large web services like Twitter.
In TCP / IP communication, a computer on the network can be uniquely identified if there is an IP address, but it is not possible to determine which program of the computer to deliver the communication packet to by the IP address alone. Use the port number to determine which program to pass the communication packet to.
Recommended Posts