Use Node.js to display hello world on the console
Use Visual Studio Code (hereinafter VSCode) Use Node.js container with Docker
--A mechanism that allows javascript, which is originally a client-side language, to be used on the server side (* 1) ――You can create various things such as websites, web applications, smartphone sites, games, etc. => Paypal, uver, etc. are also made with Node.js! ――We are good at processing a large amount of data, and the processing speed is fast.
Install Docker, a VScode extension, in advance. (* 2)
Create a Nodejs-sample-app
(any folder name is fine) folder.
Create a docker-compose.yml
file under it.
This is a standard file when downloading a container to docker, so the file name is fixed. A yml file is a text file written according to the YAML format, and is used for data transfer.
This time, it is described as follows.
docker-compose.yml
version: "3"
services:
node:
image: node:14.15
volumes:
- .:/project
tty: true
working_dir: /project
command: bash
Meaning of each item
Using the terminal function of VSCode, enter the Nodejs-sample-app
folder and type the following command to download Node.js to docker.
docker-compose up -d
image
I opened docker and it looks like this!
Select Attach shell to enter the container (see photo)
You can switch between the local terminal and the container terminal here
Create a app.js
file in the same hierarchy as docker-compose.yml
.
This time it is described as follows
app.js
console.log("Hello nodejs");
At the terminal inside the container
$ node app.js
When you type the command and the following is displayed, Node.js environment construction is complete!
![スクリーンショット_2021-01-16_12_47_28.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/663834/a2844d6f-68de-12c5-7c35-d6baf8f095fb.jpeg)
Recommended Posts