Create a simple full stack with JavaScript¶
Objectives¶
With this tutorial, you will create a simple full stack with JavaScript.
Prerequisites¶
The following prerequisites must be filled to run this service:
- Docker must be installed.
- Docker Compose must be installed (it should be installed by default with Docker in most cases).
- Visual Studio Code must be installed.
- Dev containers must be installed in Visual Studio Code.
Steps¶
Initiate the dev container¶
- Add devcontainer configuration files by clicking on the bottom left corner of Visual Studio Code and selecting "Remote-Containers: Add Development Container Configuration Files...".
- Select "Node" in the list of available containers.
- Select the latest version of Node (18 at the time of this commit) in the list of available versions.
- Select additional features to install:
- Docker (Docker-in-Docker)
- Click on "OK"
- Select the "Keep Default" option.
- Add Visual Studio Code Extensions to the
devcontainer.json
by clicking on the extensions tab on the left.- Search for "JavaScript (ES6) code snippets", click on the gear icon (⚙️) and select "Add to devcontainer.json"
- (Optional) Search for "GitHub Copilot", click on the gear icon (⚙️) and select "Add to devcontainer.json".
- Remove unnecessary comments from the
devcontainer.json
file.
Your devcontainer.json
file should look like that:
The highlighted parameters are for a late operation in this tutorial.
Your folder should look like that:
Test the development environment¶
Open the devcontainer by clicking on the bottom left corner of Visual Studio Code and selecting "Remote-Containers: Reopen in Container".
Open a terminal in Visual Studio Code and run the following command:
You should see the version of Node installed in the container. And it should be 18 or higher.
Create your first Node.js application¶
Create a new file named index.js
and add the following code:
index.js | |
---|---|
Open a terminal in Visual Studio Code and run the following command:
You should see the following output:
Get around the event loop¶
Modify the index.js
file to add the following code:
index.js | |
---|---|
Open a terminal in Visual Studio Code and run the following command:
You should see the following output:
As you can see, the process.on("exit")
callback is called after the console.log("Hello World!")
statement.
Know about the eventEmitter¶
Modify the index.js
file to add the following code:
index.js | |
---|---|
Open a terminal in Visual Studio Code and run the following command:
You should see the following output:
As you can see, the eventEmitter.on("start")
callback is called twice, but the eventEmitter.on("end")
callback is called only once.
Is there a file system ?¶
Create a new file named text.txt
and add the following text:
text.txt | |
---|---|
Modify the index.js
file to add the following code:
index.js | |
---|---|
Open a terminal in Visual Studio Code and run the following command:
You should see the following output:
As you can see, the console.log("Am I the second message ?")
statement is called before the readFile("./text.txt")
callback.
Modules¶
Create a new file named math.js
and add the following code:
math.js | |
---|---|
Modify the index.js
file to add the following code:
index.js | |
---|---|
Open a terminal in Visual Studio Code and run the following command:
You should see the following output:
initiate NPM¶
Open a terminal in Visual Studio Code and run the following command:
Your folder should look like that:
Open the terminal in Visual Studio Code and run the following command:
You should see the following output:
Wow, you can run a script without writing it !
Install a package¶
Open a terminal in Visual Studio Code and run the following command:
Your folder should look like that:
Use a package¶
Modify the index.js
file to add the following code:
Create a new file named home.html
and add the following code:
Modify the package.json
file to add the following code:
package.json | |
---|---|
Open a terminal in Visual Studio Code and run the following command:
Open your browser and go to http://localhost:3000. You should see the Hello world page.
Create a Dockerfile¶
Create a new file named Dockerfile
and add the following code:
Run the following command to build the image:
Run the following command to run the container:
Open your browser and go to http://localhost:3000. You should see the Hello world page.