[LINUX] Try to create an HTTP server using Node.js


This article is written by a student studying JS instead of a memo. Don't expect the content.

1. Build a virtual environment

This time it will be done on Ubuntu, so start the virtual environment with iTerm2.

Where it started

  1. Virtual Box 2.Vagrant I use Ubuntu in a virtual environment that uses two software. cd ~/vagrant/ubuntu vagrant up vagrant ssh

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.

2. Prepare a template for the program

Write the following in the directory.

echo "'use strict';" >file name

The first line is written when starting a new project with yarn.

3. Write a program

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']);
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.


    '<!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.

4. Try starting the server

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!!

What is yarn

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.

What is an http module?

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.

What is a port? (Reference: https://www.nic.ad.jp/ja/basics/terms/port-number.html)

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

Try to create an HTTP server using Node.js
Easy to create API server using go-json-rest module
Try to operate an Excel file using Python (Pandas / XlsxWriter) ①
I tried to output the access log to the server using Node.js
Write an HTTP / 2 server in Python
How to create an email user
[Sakura rental server] Try using flask.
Try to get statistics using e-Stat
Try to detect fusion movement using AnyMotion
Try using the Python web framework Django (1)-From installation to server startup
Try to operate Excel using Python (Xlwings)
Create an application using the Spotify API
Try using n to downgrade the version of Node.js you have installed
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
Try to generate an image with aliasing
Try to download Youtube videos using Pytube
I've built an environment to try using TensorFlow on mac, so my memo
Try to create a Todo management site using WebSocket with Django (Swamp Dragon)
I tried to create an environment to check regularly using Selenium with AWS Fargate
How to create an OCF compliant resource agent
Create an AWS GPU instance to train StyleNet
Try to create a new command on linux
Launch an HTTP server in the current directory
Create an account from CentOS mail server construction
Create a "Hello World" (HTTP) server with Tornado
Try using an object-oriented class in R (R6 method)
Introduction to Scapy ② (ICMP, HTTP (TCP) transmission using Scapy)
Create an API server quickly with Python + Falcon
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using PDFMiner
Try using geopandas
Try using Selenium
Try using pandas.DataFrame
I tried to operate from Postman using Cisco Guest Shell as an API server
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
I get an error when I try to raise Python to 3 series using pyenv on Catalina
Create an animated GIF local server with Python + Flask
How to create an article from the command line
[Blender x Python] How to create an original object
Fatal error in launcher: Unable to create process using'"'
Try to dynamically create a Checkbutton with Python's Tkinter
(Python) Try to develop a web application using Django
Create an image file using PIL (Python Imaging Library).
Try to make RESTful API with MVC using Flask 1.0.2
An easy way to create an import module with jupyter
Fatal error in launcher: Unable to create process using'"'
I tried to get an AMI using AWS Lambda
Try to delete tweets in bulk using Twitter API
How to make an HTTPS server with Go / Gin
Preparing to use Ansible on an existing Linux server
How to create an image uploader in Bottle (Python)
Create a web server in Go language (net / http) (1)
How to create a simple TCP server / client script
Try to extract high frequency words using NLTK (python)
[Machine learning] Try to detect objects using Selective Search
Try to solve Sudoku at explosive speed using numpy