Node.js

-->
  1. Node Js Tutorialspoint
  2. Node Js Download Windows
  3. Node Js Express Example

Node.js is a free, open-sourced, cross-platform JavaScript run-time environment that lets developers write command line tools and server-side scripts outside of a browser. Download Node (LTS) Get Current.

If you're brand new to using Node.js, this guide will help you to get started with some basics.

Prerequisites

  1. What is a Node.js File? Node.js files contain tasks that will be executed on certain events; A typical event is someone trying to access a port on the server; Node.js files must be initiated on the server before having any effect; Node.js files have extension '.js'.
  2. Here’s a formal definition as given on the official Node.js website: Node.js® is a.
  3. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux. Node.js also provides a rich library of various JavaScript modules which simplifies the development of web applications using Node.js to a great extent. Node.js = Runtime Environment + JavaScript Library Features of.

This guide assumes that you've already completed the steps to set up your Node.js development environment on native Windows, including:

  • Install a Node.js version manager.
  • Install Visual Studio Code.

Installing Node.js directly on Windows is the most straightforward way to get started performing basic Node.js operations with a minimal amount of set up.

Once you are ready to use Node.js to develop applications for production, which typically involves deploying to a Linux server, we recommend that you set up your Node.js development environment with WSL2. Though it's possible to deploy web apps on Windows servers, it is much more common to use Linux servers to host your Node.js apps.

Types of Node.js applications

Node.js is a JavaScript runtime primarily used for creating web applications. Put another way, it's a server-side implementation of JavaScript used for writing the backend of an application. (Though many Node.js frameworks can also handle the frontend.) Here are a few examples of what you might create with Node.js.

  • Single-page apps (SPAs): These are web apps that work inside a browser and don't need to reload a page every time you use it to get new data. Some example SPAs include social networking apps, email or map apps, online text or drawing tools, etc.
  • Real-time apps (RTAs): These are web apps that enable users to receive information as soon as it's published by an author, rather than requiring that the user (or software) check a source periodically for updates. Some example RTAs include instant messaging apps or chat rooms, online multiplayer games that can be played in the browser, online collaboration docs, community storage, video conference apps, etc.
  • Data streaming apps: These are apps (or services) that send data/content as it arrives (or is created) while keeping the connection open to continue downloading further data, content, or components as needed. Some examples include video- and audio-streaming apps.
  • REST APIs: These are interfaces that provide data for someone else's web app to interact with. For example, a Calendar API service could provide dates and times for a concert venue that could be used by someone else's local events website.
  • Server-side rendered apps (SSRs): These web apps can run on both the client (in your browser / the front-end) and the server (the back-end) allowing pages that are dynamic to display (generate HTML for) whatever content is known and quickly grab content that is not known as it's available. These are often referred to as “isomorphic” or “universal” applications. SSRs utilize SPA methods in that they don't need to reload every time you use it. SSRs, however, offer a few benefits that may or may not be important to you, like making content on your site appear in Google search results and providing a preview image when links to your app are shared on social media like Twitter or Facebook. The potential drawback being that they require a Node.js server constantly running. In terms of examples, a social networking app that supports events that users will want to appear in search results and social media may benefit from SSR, while an email app may be fine as an SPA. You can also run server-rendered no-SPA apps, which my be something like a WordPress blog. As you can see, things can get complicated, you just need to decide what's important.
  • Command line tools: These allow you to automate repetitive tasks and then distribute your tool across the vast Node.js ecosystem. An example of a command line tool is cURL, which stand for client URL and is used to download content from an internet URL. cURL is often used to install things like Node.js or, in our case, a Node.js version manager.
  • Hardware programming: While not quite as popular as web apps, Node.js is growing in popularity for IoT uses, such as collecting data from sensors, beacons, transmitters, motors, or anything that generates large amounts of data. Node.js can enable data collection, analyzing that data, communicating back and forth between a device and server, and taking action based on the analysis. NPM contains more than 80 packages for Arduino controllers, raspberry pi, Intel IoT Edison, various sensors, and Bluetooth devices.

Try using Node.js in VS Code

  1. Open your command line (Command prompt, PowerShell, or whatever you prefer) and create a new directory: mkdir HelloNode, then enter the directory: cd HelloNode

  2. Create a JavaScript file named 'app.js' with a variable named 'msg' inside: echo var msg > app.js

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

  4. 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:

  5. 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.

  6. In the terminal, enter: node app.js. You should see the output: 'Hello World'.

Note

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.

Tip

Try the new Windows terminal if you plan to use multiple command lines (Ubuntu, PowerShell, Windows Command Prompt, etc) or if you want to customize your terminal, including text, background colors, key bindings, multiple window panes, etc.

Set up a basic web app framework by using Express

Express is a minimal, flexible, and streamlined Node.js framework that makes it easier to develop a web app that can handle multiple types of requests, like GET, PUT, POST, and DELETE. Express comes with an application generator that will automatically create a file architecture for your app.

To create a project with Express.js:

  1. Open your command line (Command Prompt, Powershell, or whatever you prefer).
  2. Create a new project folder: mkdir ExpressProjects and enter that directory: cd ExpressProjects
  3. Use Express to create a HelloWorld project template: npx express-generator HelloWorld --view=pug

Note

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.

  1. 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:

    • bin. 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.
    • public. 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.
    • routes. 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.
    • views. 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.
    • app.js. 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.
    • package.json. Contains the project description, scripts manager, and app manifest. Its main purpose is to track your app's dependencies and their respective versions.
  2. You now need to install the dependencies that Express uses in order to build and run your HelloWorld Express app (the packages used for tasks like running the server, as defined in the package.json file). Inside VS Code, open your terminal by selecting View > Terminal (or select Ctrl+`, using the backtick character), be sure that you're still in the 'HelloWorld' project directory. Install the Express package dependencies with:

  1. At this point you have the framework set up for a multiple-page web app that has access to a large variety of APIs and HTTP utility methods and middleware, making it easier to create a robust API. Start the Express app on a virtual server by entering:

Tip

The DEBUG=myapp:* part of the command above means you are telling Node.js that you want to turn on logging for debugging purposes. Remember to replace 'myapp' with your app name. You can find your app name in the package.json file under the 'name' property. Using npx cross-env sets the DEBUG environment variable in any terminal, but you can also set it with your terminal specific way. The npm start command is telling npm to run the scripts in your package.json file.

  1. You can now view the running app by opening a web browser and going to: localhost:3000 Weather pro power awning parts.

  2. Now that your HelloWorld Express app is running locally in your browser, try making a change by opening the 'views' folder in your project directory and selecting the 'index.pug' file. Once open, change h1= title to h1= 'Hello World!' and selecting Save (Ctrl+S). View your change by refreshing the localhost:3000 URL on your web browser.

  3. To stop running your Express app, in your terminal, enter: Ctrl+C

Try using a Node.js module

Node.js has tools to help you develop server-side web apps, some built in and many more available via npm. These modules can help with many tasks:

ToolUsed for
gm, sharpImage manipulation, including editing, resizing, compression, and so on, directly in your JavaScript code
PDFKitPDF generation
validator.jsString validation
imagemin, UglifyJS2Minification
spritesmithSprite sheet generation
winstonLogging
commander.jsCreating command-line applications

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

  1. In your command line, open the Node.js CLI. You'll see the > prompt letting you know you're using Node.js after entering: node

  2. To identify the operating system you are currently using (which should return a response letting you know that you're on Windows), enter: os.platform()

  3. To check your CPU's architecture, enter: os.arch()

  4. To view the the CPUs available on your system, enter: os.cpus()

  5. Leave the Node.js CLI by entering .exit or by selecting Ctrl+C twice.

    Tip

    You can use the Node.js OS module to do things like check the platform and return a platform-specific variable: Win32/.bat for Windows development, darwin/.sh for Mac/unix, Linux, SunOS, and so on (for example, var isWin = process.platform 'win32';).

Next steps

In this guide, you learned a few basic things about what you can do with Node.js, tried using the Node.js command line in VS Code, created a simple web app with Express.js and ran it locally in your web browser, and then tried using a few of the built-in Node.js modules. To learn more about how to install and use some popular Node.js web frameworks, continue to the next guide which covers Next.js (a server-rendered web framework based on React), Nuxt.js (a server-rendered web framework based on Vue), and Gatsby (a statically-rendered web framework based on React). You can also skip to learning about how to work with MongoDB or PostgreSQL databases or Docker containers.

-->

Get started with Azure App Service by creating a Node.js/Express app locally using Visual Studio Code and then deploying the app to the cloud. Because you use a free App Service tier, you incur no costs to complete this quickstart.

Prerequisites

  • An Azure account with an active subscription. Create an account for free.
  • Node.js and npm. Run the command node --version to verify that Node.js is installed.
  • Visual Studio Code.
  • The Azure App Service extension for Visual Studio Code.

Clone and run a local Node.js application

  1. On your local computer, open a terminal and clone the sample repository:

  2. Navigate into the new app folder:

  3. Start the app to test it locally:

  4. Open your browser and navigate to http://localhost:1337. The browser should display 'Hello World!'.

  5. Press Ctrl+C in the terminal to stop the server.

Deploy the app to Azure

In this section, you deploy your Node.js app to Azure using VS Code and the Azure App Service extension.

  1. In the terminal, make sure you're in the nodejs-docs-hello-world folder, then start Visual Studio Code with the following command:

  2. In the VS Code activity bar, select the Azure logo to show the AZURE APP SERVICE explorer. Select Sign in to Azure.. and follow the instructions. (See Troubleshooting Azure sign-in below if you run into errors.) Once signed in, the explorer should show the name of your Azure subscription.

  3. In the AZURE APP SERVICE explorer of VS Code, select the blue up arrow icon to deploy your app to Azure. (You can also invoke the same command from the Command Palette (Ctrl+Shift+P) by typing 'deploy to web app' and choosing Azure App Service: Deploy to Web App).

  4. Choose the nodejs-docs-hello-world folder.

  5. Choose a creation option based on the operating system to which you want to deploy:

    • Linux: Choose Create new Web App
    • Windows: Choose Create new Web App.. Advanced
  6. Type a globally unique name for your web app and press Enter. The name must be unique across all of Azure and use only alphanumeric characters ('A-Z', 'a-z', and '0-9') and hyphens ('-').

  7. If targeting Linux, select a Node.js version when prompted. An LTS version is recommended.

  8. If targeting Windows, follow the additional prompts:

    1. Select Create a new resource group, then enter a name for the resource group, such as AppServiceQS-rg.
    2. Select Windows for the operating system.
    3. Select Create new App Service plan, then enter a name for the plan (such as AppServiceQS-plan), then select F1 Free for the pricing tier.
    4. Choose Skip for now when prompted about Application Insights.
    5. Choose a region near you or near resources you wish to access.
  9. After you respond to all the prompts, VS Code shows the Azure resources that are being created for your app in its notification popup.

    When deploying to Linux, select Yes when prompted to update your configuration to run npm install on the target Linux server.

  10. Select Yes when prompted with Always deploy the workspace 'nodejs-docs-hello-world' to (app name)'. Selecting Yes tells VS Code to automatically target the same App Service Web App with subsequent deployments.

  11. If deploying to Linux, select Browse Website in the prompt to view your freshly deployed web app once deployment is complete. The browser should display 'Hello World!'

  12. If deploying to Windows, you must first set the Node.js version number for the web app:

    1. In VS Code, expand the node for the new app service, right-click Application Settings, and select Add New Setting..:

    2. Enter WEBSITE_NODE_DEFAULT_VERSION for the setting key.

    3. Enter 10.15.2 for the setting value.

    4. Right-click the node for the app service and select Restart

    5. Right-click the node for the app service once more and select Browse Website.

Troubleshooting Azure sign-in

If you see the error 'Cannot find subscription with name [subscription ID]' when signing into Azure, it might be because you're behind a proxy and unable to reach the Azure API. Configure HTTP_PROXY and HTTPS_PROXY environment variables with your proxy information in your terminal using export.

If setting the environment variables doesn't correct the issue, contact us by selecting the I ran into an issue button above.

Update the app

You can deploy changes to this app by making edits in VS Code, saving your files, and then using the same process as before only choosing the existing app rather than creating a new one.

Viewing Logs

You can view log output (calls to console.log) from the app directly in the VS Code output window.

  1. In the AZURE APP SERVICE explorer, right-click the app node and choose Start Streaming Logs.

  2. When prompted, choose to enable logging and restart the application. Once the app is restarted, the VS Code output window opens with a connection to the log stream.

  3. After a few seconds, the output window shows a message indicating that you're connected to the log-streaming service. You can generate more output activity by refreshing the page in the browser.

Next steps

Congratulations, you've successfully completed this quickstart!

Check out the other Azure extensions.

Or get them all by installing theNode Pack for Azure extension pack.

Prerequisites

If you don't have an Azure account, sign up today for a free account with $200 in Azure credits to try out any combination of services.

You need Visual Studio Code installed along with Node.js and npm, the Node.js package manager.

You will also need to install the Azure App Service extension, which you can use to create, manage, and deploy Linux Web Apps on the Azure Platform as a Service (PaaS).

Sign in

Once the extension is installed, log into your Azure account. In the Activity Bar, select the Azure logo to show the AZURE APP SERVICE explorer. Select Sign in to Azure.. and follow the instructions.

Troubleshooting

If you see the error 'Cannot find subscription with name [subscription ID]', it might be because you're behind a proxy and unable to reach the Azure API. Configure HTTP_PROXY and HTTPS_PROXY environment variables with your proxy information in your terminal using export.

If setting the environment variables doesn't correct the issue, contact us by selecting the I ran into an issue button below.

Prerequisite check

Before you continue, ensure that you have all the prerequisites installed and configured.

In VS Code, you should see your Azure email address in the Status Bar and your subscription in the AZURE APP SERVICE explorer.

Create your Node.js application

Next, create a Node.js application that can be deployed to the Cloud. This quickstart uses an application generator to quickly scaffold out the application from a terminal.

Tip

If you have already completed the Node.js tutorial, you can skip ahead to Deploy to Azure.

Scaffold a new application with the Express Generator

Express is a popular framework for building and running Node.js applications. You can scaffold (create) a new Express application using the Express Generator tool. The Express Generator is shipped as an npm module and can be run directly (without installation) by using the npm command-line tool npx.

The --view pug --git parameters tell the generator to use the pug template engine (formerly known as jade) and to create a .gitignore file.

To install all of the application's dependencies, go to the new folder and run npm install.

Run the application

Next, ensure that the application runs. From the terminal, start the application using the npm start command to start the server.

Now, open your browser and navigate to http://localhost:3000, where you should see something like this:

Deploy to Azure

In this section, you deploy your Node.js app using VS Code and the Azure App Service extension. This quickstart uses the most basic deployment model where your app is zipped and deployed to an Azure Web App on Linux.

Deploy using Azure App Service

First, open your application folder in VS Code.

In the AZURE APP SERVICE explorer, select the blue up arrow icon to deploy your app to Azure.

Tip

You can also deploy from the Command Palette (CTRL + SHIFT + P) by typing 'deploy to web app' and running the Azure App Service: Deploy to Web App command.

Node.js
  1. Choose the directory that you currently have open, myExpressApp.

  2. Choose Create new Web App, which deploys to App Service on Linux by default.

  3. Type a globally unique name for your Web App and press ENTER. Valid characters for an app name are 'a-z', '0-9', and '-'.

  4. Choose your Node.js version, LTS is recommended.

    The notification channel shows the Azure resources that are being created for your app.

  5. Select Yes when prompted to update your configuration to run npm install on the target server. Your app is then deployed.

  6. When the deployment starts, you're prompted to update your workspace so that later deployments will automatically target the same App Service Web App. Choose Yes to ensure your changes are deployed to the correct app.

Tip

Be sure that your application is listening on the port provided by the PORT environment variable: process.env.PORT.

Browse the app in Azure

Once the deployment completes, select Browse Website in the prompt to view your freshly deployed web app.

Troubleshooting

Node Js Tutorialspoint

Node.js

If you see the error 'You do not have permission to view this directory or page.', then the application probably failed to start correctly. Head to the next section and view the log output to find and fix the error. If you aren't able to fix it, contact us by selecting the I ran into an issue button below. We're happy to help!

Update the app

You can deploy changes to this app by using the same process and choosing the existing app rather than creating a new one.

Viewing Logs

In this section, you learn how to view (or 'tail') the logs from the running App Service app. Any calls to console.log in the app are displayed in the output window in Visual Studio Code.

Find the app in the AZURE APP SERVICE explorer, right-click the app, and choose Start Streaming Logs.

The VS Code output window opens with a connection to the log stream.

After a few seconds, you'll see a message indicating that you're connected to the log-streaming service. Refresh the page a few times to see more activity.

Node Js Download Windows

Next steps

Node.js

Congratulations, you've successfully completed this quickstart!

Next, check out the other Azure extensions.

Node Js Express Example

Or get them all by installing theNode Pack for Azure extension pack.