Node Getting Started

Posted on  by admin

Download Node.js

The official Node.js website has installation instructions for Node.js:

  1. Once you have downloaded and installed Node.js on your computer, let's try to display "Hello World" in a web browser.
  2. Create a Node.js file named "myfirst.js", and add the following code:.
  3. var http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hello World!'
  4. Save the file on your computer: C:\Users\Your Name\myfirst.js. The code tells the computer to write "Hello World!"

if anyone (e.g. a web browser) tries to access your computer on port 8080.

A Vast Number of Libraries

For now, you do not have to understand the code. It will be explained later.

Command Line Interface

Node.js files must be initiated in the "Command Line Interface" program of your computer.

How to open the command line interface on your computer depends on the operating system. For Windows users, press the start button and look for "Command Prompt", or simply write "cmd" in the search field.

Navigate to the folder that contains the file "myfirst.js", the command line interface window should look something like this:.

Initiate the Node.js File

The file you have just created must be initiated by Node.js before any action can take place.

Start your command line interface, write node myfirst.js and hit enter:. Initiate "myfirst.js":. Now, your computer works as a server! If anyone tries to access your computer on port 8080, they will get a "Hello World!"

message in return! Start your internet browser, and type in the address: http://localhost:8080.

Installing on Node.js on Windows or on Windows Subsystem for Linux. Open your command line and create a new directory: mkdir HelloNode, then enter the directory: cd HelloNode. Create a JavaScript file named "app.js" with a variable named "msg" inside: echo var msg > app.js.

Getting Started

Open the directory and your app.js file in VS Code using the command: code . Add a simple string variable ("Hello World"), then send the contents of the string to your console by entering this in your "app.js" file:.

To run your "app.js" file with Node.js.

Open your terminal right inside VS Code by selecting View > Terminal (or select Ctrl+`, using the backtick character).

If you need to change the default terminal, select the dropdown menu and choose Select Default Shell.

In the terminal, enter: node app.js. You should see the output: "Hello World". Notice that when you type console in your 'app.js' file, VS Code displays supported options related to the console object for you to choose from using IntelliSense.

Try experimenting with Intellisense using other JavaScript objects. Open your command line (Command Prompt, Powershell, or whatever you prefer). Create a new project folder: mkdir ExpressProjects and enter that directory: cd ExpressProjects.

An Example Node.js Application

Use Express to create a HelloWorld project template: npx express-generator HelloWorld --view=pug.

We are using the npx command here to execute the Express.js Node package without actually installing it (or by temporarily installing it depending on how you want to think of it). If you try to use the express command or check the version of Express installed using: express --version, you will receive a response that Express cannot be found.

If you want to globally install Express to use over and over again, use: npm install -g express-generator.

You can view a list of the packages that have been installed by npm using npm list.

They'll be listed by depth (the number of nested directories deep).

Packages that you installed will be at depth 0. That package's dependencies will be at depth 1, further dependencies at depth 2, and so on.

To learn more, see Difference between npx and npm? on StackOverflow. Examine the files and folders that Express included by opening the project in VS Code, with: code .

The files that Express generates will create a web app that uses an architecture that can appear a little overwhelming at first.

You'll see in your VS Code Explorer window (Ctrl+Shift+E to view) that the following files and folders have been generated:.

Contains the executable file that starts your app.

It fires up a server (on port 3000 if no alternative is supplied) and sets up basic error handling.

Node.js Frameworks and Tools

Contains all the publicly accessed files, including JavaScript files, CSS stylesheets, font files, images, and any other assets that people need when they connect to your website.

Contains all the route handlers for the application. Two files, index.js and users.js, are automatically generated in this folder to serve as examples of how to separate out your application’s route configuration.

Contains the files used by your template engine. Express is configured to look here for a matching view when the render method is called.

The default template engine is Jade, but Jade has been deprecated in favor of Pug, so we used the --view flag to change the view (template) engine.

You can see the --view flag options, and others, by using express --help.

The starting point of your app. It loads everything and begins serving user requests. It's basically the glue that holds all the parts together.

Contains the project description, scripts manager, and app manifest.