Select Node.js when prompted for the application platform. Select either Yes or No when prompted to include Docker Compose files. Compose is typically used when running multiple containers at once. Enter 3000 when prompted for the application port. Node.js in a container. In this guide you will learn how to: Create a Dockerfile file for an Express Node.js service container; Build, run, and verify the functionality of the service; Debug the service running within a container; Prerequisites. Both Docker and the VS Code Docker extension must be installed as described in the overview; Node.js. (1) Quick way to install Node.js. Go to the official page of Node.js and download the install package for your operating system. Use the LTS version not the current one. After the download is complete install the package like any other app on your Mac or PC; Next you can go to your Terminal program of choice. It's simple to run app.js with Node.js. From a terminal, just type: node app.js. You should see 'Hello World' output to the terminal and then Node.js returns. Integrated Terminal. VS Code has an integrated terminal which you can use to run shell commands. You can run Node.js directly from there and avoid switching out of VS Code while running.
Before creating an actual 'Hello, World!' application using Node.js, let us see the components of a Node.js application. A Node.js application consists of the following three important components −
Creating Node.js ApplicationStep 1 - Import Required Module
We use the require directive to load the http module and store the returned HTTP instance into an http variable as follows −
Running App Built With Node.js On Macbook![]() Step 2 - Create Server
We use the created http instance and call http.createServer() method to create a server instance and then we bind it at port 8081 using the listen method associated with the server instance. Pass it a function with parameters request and response. Write the sample implementation to always return 'Hello World'.
Install Node Js On Mac
The above code is enough to create an HTTP server which listens, i.e., waits for a request over 8081 port on the local machine.
Step 3 - Testing Request & Response
Let's put step 1 and 2 together in a file called main.js and start our HTTP server as shown below −
Basic Node Js App
Now execute the main.js to start the server as follows −
Verify the Output. Server has started.
Make a Request to the Node.js Server
Open http://127.0.0.1:8081/ in any browser and observe the following result.
Congratulations, you have your first HTTP server up and running which is responding to all the HTTP requests at port 8081.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |