How To Create Simple Node Js Application
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
- Feedback
- Edit
Quickstart: Create your first Node.js app with Visual Studio
- 5 minutes to read
Thank you.
In this 5-10 minute introduction to the Visual Studio integrated development environment (IDE), you create a simple Node.js web app.
Prerequisites
Before you begin, install Visual Studio and set up your Node.js environment.
Install Visual Studio and the Node.js workload
If you haven't yet installed Visual Studio:
-
Go to the Visual Studio downloads page to install Visual Studio 2022 for free.
-
In the Visual Studio Installer, select the Node.js development workload, and select Install.
If you have Visual Studio installed already.
-
In Visual Studio, go to Tools > Get Tools and Features.
-
In the Visual Studio Installer, choose the Node.js development workload, and select Modify to download and install the workload.
Set up your Node.js environment
Install the LTS version of the Node.js runtime. The LTS version has the best compatibility with other frameworks and libraries.
Although Node.js is built for 32-bit and 64-bit architectures, the Node.js installer only supports one version at a time.
Visual Studio usually detects your installed runtime, but if not, you can configure your project to reference the installed runtime:
-
After creating your project, right-click the project node.
-
Select Properties, and set the Node.exe path. You can use a global Node.js installation, or specify the path to a local interpreter for any Node.js project.
Install Visual Studio
If you haven't already installed Visual Studio 2019, go to the Visual Studio downloads page to install it for free.
Install Visual Studio
If you haven't already installed Visual Studio 2017, go to the Visual Studio downloads page to install it for free.
Set up your Node.js environment
Visual Studio can help set up your environment, including installing tools common with Node.js development.
-
In Visual Studio, go to Tools > Get Tools and Features.
-
In the Visual Studio Installer, choose the Node.js development workload and select Modify to download and install the workload.
-
Install the LTS version of the Node.js runtime. We recommend the LTS version for best compatibility with outside frameworks and libraries.
Although Node.js is built for 32-bit and 64-bit architectures, the Node.js installer only supports one version installed at a time.
-
If Visual Studio doesn't detect your installed runtime (it generally does), configure your project to reference the installed runtime:
-
After you create your project, right-click the project node.
-
Select Properties and set the Node.exe path. You can use a global installation of Node.js or specify the path to a local interpreter in each of your Node.js projects.
-
Create your app project
In Visual Studio, create a new Node.js project.
-
Start Visual Studio, and then press Esc to close the start window.
-
Press Ctrl+Q, and then type node.js in the search box.
-
Select Blank Node.js Web Application.
-
In the dialog box, select Create.
-
Press Esc to close the start window.
-
Press Ctrl + Q to open the search box, then type Node.js.
-
Choose Blank Node.js Web Application (JavaScript). In the dialog, select Create.
-
From the top menu bar, choose File > New > Project.
-
In the left pane of the New Project dialog, expand JavaScript and choose Node.js.
-
In the middle pane, choose Blank Node.js Web application and select OK.
Visual Studio creates and opens the project. The project's server.js file opens in the editor.
If you don't see the Blank Node.js Web Application project template, you need to add the Node.js development workload. For instructions, see Prerequisites.
Explore the IDE
Visual Studio can help set up your environment, including installing tools common with Node.js development.
-
In the right pane, look at the Solution Explorer.
- At the top level is a solution, which by default has the same name as your project. A solution, represented by a .sln file on disk, is a container for one or more related projects.
- Your project, with the name you used when you set it up, is highlighted in bold. On disk, the project is represented by a .njsproj file in your project folder.
- The npm node shows installed npm packages. You can right-click the npm node to search for and install npm packages by using a dialog.
-
To install npm packages or Node.js commands from a command prompt, right-click the project node and choose Open Command Prompt Here.
-
To test navigation to source code, in the open server.js file, select
createServerand press F12, or right-clickcreateServerand select Go To Definition from the context menu. This command takes you to the definition of thecreateServerfunction in http.d.ts.
-
Back in server.js, locate this line of code:
res.end('Hello World\n');, and modify it to:res.end('Hello World\n' + res.connection.When you type
connection., IntelliSense provides options to autocomplete the code entry.
-
Choose
localPort, and type);to complete the statement:res.end('Hello World\n' + res.connection.localPort);
-
In the right pane, look at the Solution Explorer.
- At the top level is a solution, which by default has the same name as your project. A solution, represented by a .sln file on disk, is a container for one or more related projects.
- Your project, with the name you used when you set it up, is highlighted in bold. On disk, the project is represented by a .njsproj file in your project folder.
- The npm node shows installed npm packages. You can right-click the npm node to search for and install npm packages by using a dialog.
-
To install npm packages or Node.js commands from a command prompt, right-click the project node and choose Open Command Prompt Here from the context menu.
-
To test navigation to source code, in the open server.js file, select http.createServer and press F12 or choose Go To Definition from the right-click context menu. This command takes you to the definition of the
createServerfunction in http.d.ts.
-
Back in server.js, locate this line of code:
res.end('Hello World\n');, and modify it to:res.end('Hello World\n' + res.connection.When you type connection., IntelliSense provides options to autocomplete the code entry.
-
Choose localPort, and type
);to complete the statement:res.end('Hello World\n' + res.connection.localPort);
Run the app
-
Press Ctrl+F5 or select Debug > Start Without Debugging to run the app.
The app opens in a browser.
-
In the browser, verify that you see a Hello World message and the local port number.
Congratulations! You created a simple Node.js app with Visual Studio. To delve deeper, continue to the Tutorials section of the table of contents.
Next steps
Feedback
How To Create Simple Node Js Application
Source: https://docs.microsoft.com/en-us/visualstudio/ide/quickstart-nodejs
Posted by: covarrubiasdond1949.blogspot.com

0 Response to "How To Create Simple Node Js Application"
Post a Comment