Node or Node.js is an open-source, cross-platform runtime environment that allows developers to create server-side tools and applications in JavaScript. 

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

What Is NPM?

Node Package Manager (NPM) is a JavaScript package manager for the Node App platform.

NPM is the largest software registry in the world. Open-source developers use it worldwide to publish and share their code.

NPM is required for the following reasons -

  • You may use the website to search for third-party packages, create profiles, and manage your packages.
  • The command-line interface or NPM CLI allows you to communicate with NPM from a terminal.
  • The registry is a big database of JavaScript code that is open to the public.

To find the NPM CLI on your computer, run the NPM command from a terminal:

npm

For example, the following command will get displayed in the current NPM version on your system:

npm -v

NPM Usage

You can install a new package from the registry using NPM. Furthermore, NPM enables you to find and publish new node packages.

What Is Express?

Node.js leverages the Express server web application framework to create Node apps (or) web apps. You may construct a user interface with Express using a variety of front-end frameworks; this uses Pug, formerly called Jade, for its front-end framework.

Features of Express in Node App

  • Performance: Express adds a thin layer of basic web application functionality without obscuring the Node.js capabilities you already know and appreciate.
  • APIs: Using various HTTP utility methods and middleware, you can quickly and easily build a powerful API.
  • Frameworks: Express is the foundation for many prominent frameworks.
  • Web Applications: Express is a Node.js web application framework that offers a comprehensive range of functionality for both web and mobile apps.

Prerequisites

Following are the requirements for creating a Node App.

  • The Node.js development workload is installed in Visual Studio.

Download and install Visual Studio -

  1. To get Visual Studio for free, go to the Visual Studio downloads website.
  2. Select the Node.js development workload in the Visual Studio Installer and click Install.

If you already have Visual Studio installed, follow these steps -

  1. Go to Tools > Get Tools and Features in Visual Studio.
  2. To download and install the Node.js development workload, go to the Visual Studio Installer and select Modify.
  • Install the Node.js runtime:

Install the LTS version of the Node.js runtime from the Node.js website if you don't already have it. Other frameworks and libraries are also compatible with the LTS version.

The Node.js tools support the 32-bit and 64-bit architectural versions of Node.js in the Visual Studio Node.js workload. Only one version of Visual Studio is required; the Node.js installer only supports one version at a time.

The installed Node.js runtime is usually detected automatically by Visual Studio. If this isn't the case, you can set up your project to use the installed runtime:

  1. Right-click the project node and choose Properties after you've created it.
  2. Set the Node.exe path to a global or local Node.js installation in the Properties tab. In each of your Node.js projects, you can define the route to a local interpreter.

Free Course: Getting Started with NodeJS

Master the Basics of NodeJSEnroll Now
Free Course: Getting Started with NodeJS

Create a New Node.js Project for Node App

In a project, Visual Studio maintains files for a single application. Source code, resources, and configuration files are all included in the project.

Let us see how to create a new Node.js project for the Node app by taking a small project with code for a Node.js and ExpressJS Node app in the current context.

1. To close the start window, open Visual Studio and hit Esc.

2. Choose Basic Azure Node.js Express 4 Application-JavaScript from the dropdown list by pressing Ctrl+Q, typing node.js in the search field.

Install the Node.js development workload if you don't see the Basic Azure Node.js Express 4 Application option. See the Prerequisites for further information.

3. Select Create in the ‘Configure Your New Project’ dialogue box.

The new solution and project are created in Visual Studio, and the project is opened in the right pane. The app.js project file appears in the left pane of the editor.

4. In the right pane, look at the project structure in Solution Explorer.

NodeApp_1.

  • Solution (1) is at the top of the hierarchy, and it has the same name as your project by default. A solution is a container for one or more connected projects, represented by a.sln file on disc.
  • Your project (2) is highlighted in bold, with the name you gave in the ‘Configure Your New Project’ dialogue box. The project is a.njsproj file in your project folder in the file system.

By right-clicking the project and selecting Parameters from the context menu, you can see and change project properties and environment variables. Because the project file does not make any specific changes to the Node.js project source, you can use alternative development tools.

  • The NPM node (3) displays any NPM packages that have been installed. You may use a dialogue box to search for and install NPM packages by right-clicking the NPM node.

Using the package.json settings and the NPM node's right-click options, you may install and update packages.

  • Under the project node, there are four project files. The app.js project starter file is highlighted in bold.

By right-clicking any file in the project and choosing Set as Node.js startup file, you can set up a startup file.

  • NPM manages dependencies and versions for locally installed packages using the package.json file (5). See Manage NPM packages for additional details.

5. Open the NPM node to ensure all the required NPM packages are present.

If any packages are (missing), right-click the NPM node, select Install NPM Packages, and install the missing packages.

Add Some Code

The application's front-end JavaScript framework is Pug. Pug generates HTML from simple markup code.

With the code app.set('view engine', 'pug');, Pug is configured as the view engine in app.js.

1. Access the views folder in Solution Explorer, then pick index.pug to open the file.

2. Replace the contents with the markup below.

extends layout

block content

  h1= title

  p Welcome to #{title}

  script.

    var f1 = function() { document.getElementById('myImage').src='#{data.item1}' }

  script.

    var f2 = function() { document.getElementById('myImage').src='#{data.item2}' }

  script.

    var f3 = function() { document.getElementById('myImage').src='#{data.item3}' }

  button(onclick='f1()') One!

  button(onclick='f2()') Two!

  button(onclick='f3()') Three!

  p

  a: img(id='myImage' height='300' width='300' src='')

The preceding code creates an HTML page with a title and a welcome message on the fly. The page also has code that allows you to see an image that changes when you push a button.

3. Open index.js in the routes folder.

4. Before the router.get, add the following code:

var getData = function () {

    var data = {

        'item1': 'https://images.unsplash.com/photo-1563422156298-c778a278f9a5',

        'item2': 'https://images.unsplash.com/photo-1620173834206-c029bf322dba',

        'item3': 'https://images.unsplash.com/photo-1602491673980-73aa38de027a'

    }

    return data;

}

This code builds a data object that you may send to the HTML page that is dynamically produced.

5. Replace the router with a new one. Use the following to call the function:

router.get('/', function (req, res) {

    res.render('index', { title: 'Express', "data" });

});

The accompanying code uses the Express router object to set the current page and render it, sending the title and data object to the page. When index.js executes, the code defines the index.pug file as the page to load. Index.js is set as the default route in the app.js code, which isn't seen here.

There's a deliberate error in the code that contains res.render to demonstrate various Visual Studio features. IntelliSense will assist you in resolving the error so that the app can run in the following stage.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Use IntelliSense

IntelliSense is a Visual Studio feature that assists you with code writing while creating a Node App.

1. In the Visual Studio code editor, go to the code containing res.render in index.js.

2. Place your cursor after the string "data" and type get. The getData method you defined previously in the code is displayed by IntelliSense. Choose getData.

NodeApp_2

3. To make the code a function call, add parentheses: getData().

4. Before "data," remove the comma. The expression has green syntax highlighting. Hover your mouse over the syntax highlighting to see what it means.

NodeApp_3.

The message's last line informs you that the JavaScript interpreter expected a comma.

5. Pick the Error Selection tab in the lower pane, and for the type of issues reported, select Build + IntelliSense from the dropdown list.

The warning and description, as well as the filename and line number, are displayed in this window.

NodeApp_4.

6. Replace the comma before "data" in the code.

This is how the amended line of code should look: res.render('index', 'Express' as title, "data" as getData());

Run the Node App

Then, with the Visual Studio debugger attached, execute the app. To begin, you must first set a breakpoint.

Determine a Breakpoint

Breakpoints are the most fundamental aspect of a reliable debugging system. A breakpoint is a point in your running code where Visual Studio should suspend it. Variable values, memory behavior, and whether a branch of code is running can all be observed this way.

  • In index.js, click on the left before the following line of code to establish a breakpoint:

res.render('index', 'Express' as title, "data" as getData());

NodeApp_5

Run the App in Debug Mode

1. In the Debug toolbar, select a debug target, such as Web Server (Google Chrome) or Web Server (Internet Explorer) (Microsoft Edge).

NodeApp_6

Select Browse With from the debug target dropdown list if you know your preferred debug target is present on your machine, but it doesn't appear as an option. Select Set as Default from the drop-down menu for your default browser target.

2. To execute the Node app, press F5 or go to Debug > Start Debugging.

The debugger pauses at the breakpoint you specify, allowing you to analyze the status of your app.

3. Hover over getData in a DataTip to see its characteristics.

NodeApp_7

4. To continue running the software, press F5 or select Debug > Continue.

The software is accessed through a web browser. The browser window’s title should be Express, and the first paragraph should be Welcome to Express.

5. To see different images, press the One!, Two!, or Three! buttons.

NodeApp_8

6. Close the browser window.

Are you a web developer or interested in building a website? Enroll for the Full Stack Web Developer - MEAN Stack Master's Program. Explore the course preview!

Learn Node.js From the Best!

Node.js is a cross-platform server environment that uses the V8 JavaScript Engine to execute JavaScript. Node.js enables front-end and back-end code to be written in the same language. It aids in the development of efficient code for real-time applications. Node Apps can be created in Node.js using either a console-based or a web-based technique.

To master and learn more about Node App and how to implement and build dynamic applications using Node and get well versed with full-stack development, check out Simplilearn's Full Stack Web Development Certification Course in collaboration with Caltech CTME. This course will help you master both backend and frontend with tools like Hibernate, JSPs, Angular, etc.

If you want to test a few courses before you enroll, Simplilearn offers free online skill-up courses in several in-demand domains to help you advance in your career.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.