Introduction to Node. js in Windows
This article mainly introduces the simple getting started tutorial of Node. js in Windows. Node. js is a JavaScript framework for backend programming. If you need it, refer
Recently, Paypal and Netflix announced the migration to Node. js, the server-side Javascript platform has proved its value in the enterprise field. this is a small step for Node and a huge leap for Javascript! From. NET, Java, PHP, Ruby on Rails and programmers in more technical fields, all the server-side programmers will gather on this platform. as a major player like Yahoo, Walmart, and Oracle, Node is getting rid of its long-standing bad reputation for being immature and unstable. in this article, I will show you how to install Node in the Window environment. how easy is js.
Install Node. js
Make Node. installing js on Windows is a breeze. to Node. download and run the js Website ". msi "file. it will. install js and NPM (Node package management module. NPM is equivalent.. NET application NuGet Package Manager.
Run Node. js
Run Node. js is also very easy. open PowerShell and enter "node-v" to ensure that Node is in your environment variables and view the Node you are running. js version. enter "npm-v" to view the version of the Node package management tool you have installed. have you finished it? OK. Let's get started !!
Open the Notepad program and we will build our first Node. js applications. copy the following code to the Notepad program and use any file name, for example, "example. js "and save it to the folder you want:
var http = require ('http ');
Http. createServer (function (req, res ){
Res. writeHead (200, {'content-type': 'text/plain '});
Res. end ('Hello node ');
}). Listen (1337, '192. 0.0.1 ');
Now go back to PowerShell. Change the path to the location where your "example. js" file is stored and run Node!
cd C: \ Websites \ NodeTest
Node example. js
Open your web browser, and enter http: // 127.0.0.1: 1337 in the navigation bar. Does it work? Congratulations! You have run your first Node. js application!
Provide WebSite Services
You will be worried that I will leave only one "Hello World" example to close the work? If we know how to run an HTML file, it will be even better. add an "index.html" file, which can be any HTML content. It will be like this
1 2 3 4 5 6 7 8 |
<Html> <Head> <Title> Sample Node. js Website </title> </Head> <Body> <P> This is the home page for you Node. js website. </p> </Body> </Html> |
It is time to run the application. Create a new file named by any name, such as "index. js", and add the following js Code to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Var http = require ('http '); Var fs = require ('fs '); Http. createServer (function (req, res ){ Fs.readFile('index.html ', function (err, data ){ Res. writeHead (200 ,{ 'Content-type': 'text/html ', 'Content-length': data. Length }); Res. write (data ); Res. end (); }); }). Listen (1337, '192. 0.0.1 '); |
Things have become more interesting here. note that a line of "require" is added at the beginning ". you are bringing the required dependencies to your application. this is like the "using" namespace command used to call the dependent program in C.
Enter node index in PowerShell. js To Run "index. js "(do not forget to press Ctrl-C to exit the running of the previous Node application, or use a new port number this time ). in your browser, navigate to http: // 127.0.0.1: 1337 and you will see your HTML file. you may feel a little excited about this achievement, but if you think about me, you will feel complicated about it. this is only a low-level programming. If I have to consider reading/streaming media files and the status of each issue that should be sent, the world will soon be troubled. say OK to ExpressJS!
Use Node Package Manager
Node. js has a partner that makes us feel the world better. expressJS blocks the Node. the old debugging in js allows you to directly access web development. it is a web framework that allows you to build single pages, multiple pages, and hybrid web applications. without it, you are at Node. in the js field, there is no hope!
First, use NPM to install it. to this end, open PowerShell again and switch to the path of your application. enter npm install express. it will create a "node_modules" to install ExpressJS. from this perspective, your Node module will be put there, a bit like in. the "bin" directory in the. NET application, from which you can call or "require" Your dependent program.
Get started with ExpressJS
Create a new file, such as "server. js", and paste the following code:
1 2 3 4 5 6 7 8 9 10 |
Var express = require ('express '); // CREATE APP Var app = express (); // LOCATION OF STATIC CONTENT IN YOUR FILESYSTEM App. use (express. static (_ dirname )); // PORT TO LISTEN App. listen (1337 ); |
This is the dependency of calling ExpressJS, and then creating an application from it. From then on, you will be awesome! Here, we simply provide the static file service. "_ dirname" is a special variable from ExpressJS, meaning the location of the root file system. finally, you tell the application to listen on port 1337. now you have a Node that provides the static file service. js site! In addition, add some HTML files, put them in Sub-directories, and test them at http: // 127.0.0.1: 1337.
About IIS
In these examples, I have been running applications on port 1337 instead of port 80. the reason is that IIS has listened on port 80. there are many ways to make IIS and Node. js harmonious coexistence:
IISNode: This is a Node on your IIS Site. js runs like an application pool, which is similar to running PHP in IIS. in fact, Azure runs Node on its platform. js.
WinServ: It makes Node. js runs like a Windows service. it is actually a pair of nodes that are popular with NSSM (Non-Sucking Service Manager. js-friendly encapsulation. once running as a service, you can use the IIS application Request Routing (ARR) to proxy to your Node. request initiated by js application port.
About MS SQL
Many of them are Node. js-prepared ms SQL driver, some or even cross-platform. one of them can only run in a Windows environment, which is released by Windows Azure: Microsoft Driver for Node. js for SQL Server. you can start working like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Var SQL = require ('node-sqlserver '); Var connStr = "Driver = {SQL Server Native Client 11.0}; Server = (local); Database = AdventureWorks2012; Trusted_Connection = {Yes }"; Var cmd = "select top 10 FirstName, LastName FROM Person. Person "; SQL. open (connStr, function (err, conn ){ Conn. queryRaw (cmd, function (err, results ){ For (var I = 0; I <results. rows. length; I ++ ){ Console. log ( "FirstName:" + results. rows [I] [0] + "LastName:" + results. rows [I] [1]); } }); }); |
Summary
These are just fur! Working with ExpressJS, you will be able to create completely sophisticated MVC applications with routes, views, la S, services, and more components. similarly, unless you need to integrate some existing Microsoft applications or ms SQL databases, MongoDB is a good partner that can help you free up SQL when you create a Node stack. finally, you can use MEAN to create a full-stack MEAN Javascript Application, including MongoDB, ExpressJS, AngularJS, and Node. js. now the Enterprise has been directed to Node. js is moving closer. Is it time for you to assist in the same action?