Simple getting started with Node. js in Windows, and getting started with node. js

Source: Internet
Author: User
Tags install node

Simple getting started with Node. js in Windows, and getting started with node. js

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, '');

Now go back to PowerShell. Change the path to the location where your "example. js" file is stored and run Node!

cd C:\Websites\NodeTestnode example.js

Open your web browser, and enter http: // 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 contain any HTML content. It will look like this:

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:

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, '');

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: // 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:

var express = require('express'); //CREATE APPvar app = express(); //LOCATION OF STATIC CONTENT IN YOUR FILESYSTEMapp.use(express.static(__dirname)); //PORT TO LISTEN TOapp.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: // 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:

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";, 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]);    }  });});


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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.