Should I learn node js in 2019

Tutorial: Node.js for beginners

  • 6 minutes to read

If you're new to using Node.js, this guide will provide you with some basics.

requirements

If you are a beginner and are using Node.js for the first time, it is recommended that you run the installation directly on Windows. For more information, see Should I install Node.js on Windows or Windows Subsystem for Linux?

Using NodeJS with Visual Studio Code

If you haven't already installed Visual Studio Code, return to the prerequisites section above and follow the installation steps linked for Windows or WSL.

  1. Open your command line and create a new directory:, then enter the directory:

  2. Create a JavaScript file with the name "app.js" and the contained variable "msg":

  3. Open the directory and your app.js file in VS Code with the command:

  4. Add a simple string variable ("Hello World"), then send the contents of the string to the console by typing the following in the app.js file:

  5. Follow these steps to run the app.js file with Node.js. Open the Terminal in VS Code by selecting Show > terminal (or press CTRL + `(backsplash)). If you want to change the default terminal, select the drop-down menu and then Select standard shell out.

  6. Enter the following in the terminal:. You should see the following output: "Hello World".

Note

Note that when you enter any of the supported options in the app.js file, VS Code will display supported options for the object that you can select using IntelliSense. Experiment with IntelliSense using other JavaScript objects.

Build your first NodeJS web app with Express

Express is a minimal, flexible and streamlined Node.js framework that simplifies the development of a web app that can handle multiple types of requests like GET, PUT, POST and DELETE. Express includes an application generator that automatically creates a file architecture for your app.

How to create a project with Express.js

  1. Open a command line (Command Prompt, PowerShell, or whatever you prefer).

  2. Create a new project folder with and switch to the directory with.

  3. Create a HelloWorld project template with Express:

    Note

    We use the command here to run the Express.js node package without actually installing it (or by temporarily installing it, depending on how you look at it). When you try to use the command or to check the installed version of Express, you get the answer that Express cannot be found. If you want to install Express globally so that you can use it over and over again, you are using. You can use to view a list of the packages npm installed. They are listed according to their depth (number of nested directory levels). Packages that you have installed are on level 0. This package's dependencies are on level 1, other dependencies on level 2, and so on. For more information, see Difference Between NPX and NPM? on Stack Overflow.

  4. Examine the files and folders contained in Express by opening the project in VS Code:

    The files generated by Express create a web app with an architecture that can seem a bit overwhelming at first. In the VS Code window you canExplorers (displayed with CTRL + SHIFT + E) recognize that the following files and folders have been created:

    • . Contains the executable file that starts your app. Starts a server (on port 3000 if no alternative is given) and sets up basic error handling.
    • . Contains all publicly available files including JavaScript files, CSS style sheets, font files, images, and any other resources users need when connecting to your website.
    • . Contains all route handlers for the application. Two files, and, are automatically generated in this folder to serve as examples for segregating your application's route configuration.
    • . Contains the files used by your template engine. Express is configured to look for a matching view here when the render method is invoked. The default template engine is Jade, but it has been superseded by Pug, so we used the flag to change the display engine (template engine). You can view the flag options and others using.
    • . The starting point of the app. It loads everything and starts processing user requests. Basically, it holds all the different parts together like an adhesive.
    • . It contains the project description, script manager and app manifest. Its main purpose is to keep track of your app's dependencies and their respective versions.
  5. You will now need to install the dependencies used by Express to build and run your Express HelloWorld app (the packages used for tasks like running the server as defined in the file). In VS Code, open a terminal by typing Show > terminal select (or press CTRL + `(grave accent). Make sure that you are still in the "HelloWorld" project directory. Install the express package dependencies with:

  6. At this point you have the framework for a multipage web app in place with access to a wide variety of APIs and useful HTTP methods and middleware, which makes it easy to build a robust API. Start the Express app on a virtual server by entering:

    tip

    The part in the above command is telling Node.js that you want to enable logging for debugging purposes. Remember to replace "myapp" with the name of your app. You can find the app name in the file under the name property. With the environment variable is set in any terminal, you can also set it via the respective procedure of your terminal. The command tells npm to run the scripts in your file.

  7. You can now view the running app by opening and closing a web browser localhost: 3000 navigate.

  8. Now that your Express HelloWorld app is running locally in a browser, you should try making a change. To do this, open the “views” folder in the project directory and select the “index.pug” file. When it opens, change to, then select to save (CTRL + S). Indicate the change by typing the URL localhost: 3000 update in your web browser.

  9. In your terminal, enter the following to stop the Express app: CTRL + C

Trying out a Node.js module

Node.js has many tools for developing server-side web apps - some are built in and many more are available through npm. These modules can be helpful in a wide variety of tasks:

Tooluse
gm, sharpImage manipulation, including editing, resizing, compressing, and more, right in your JavaScript code
PDFKitPDF generation
validator.jsString validation
imagemin, UglifyJS2Minimization
spritesmithSprite sheet generation
WinstonLogging
commander.jsCommand line application building

We'll use the built-in OS module to get some information about your computer's operating system:

  1. At the command line, open the Node.js command line interface. After entering, the prompt will appear. It indicates that you are using Node.js.

  2. Enter the following to identify the operating system you are currently using (the response should be that you are running Windows):

  3. Enter the following to check the CPU architecture:

  4. Enter the following to see the CPUs available on your system:

  5. Exit the Node.js command line interface by typing or pressing Ctrl + C twice.

    tip

    You can use the operating system module from Node.js e.g. Use it, for example, to check the platform and return a platform-specific variable: "Win32 / .bat" for Windows development, "darwin / .sh" on Mac / UNIX, Linux, SunOS etc. (e.g.).

Is this page helpful?