A simple introductory tutorial on Node.js under Windows systems

Source: Internet
Author: User
Tags file system iis mongodb require web services install node port number ruby on rails

This article introduces a simple introductory tutorial for node.js under Windows System, Node.js is a JavaScript framework for back-end programming, and friends can refer to the following

With recent PayPal and Netflix announcements migrating to Node.js, the server-side JavaScript platform has proven its own value in the Enterprise domain. This is a small step for node, but a big leap for JavaScript! Programmers from. NET, Java, PHP, Ruby on rails, and more technology, all of the coders on the server side converge on this platform. As big players like Yahoo, Walmart, and Oracle, Node is losing its reputation for being immature and unstable. In this article, I'll show you how easy it is to install node.js in the window environment.

Install Node.js

Getting node.js installed on Windows is a trivial matter. Download and run the. msi file to the Node.js Web site. It will install Node.js and NPM (node package management module). NPM is equivalent to. NET application of the NuGet Package Manager.

Run Node.js

Running Node.js on Windows is also easy. Open PowerShell and enter "Node-v" to make sure node is in your environment variable and view the version of the node.js you are running. The same input "npm-v" to view the version of the node package management tool that you installed. Are you done yet? OK, let's start to enjoy the fun!!

Open the Notepad program and we will build our first Node.js application. Copy the following code into a notepad program, using any filename, such as "Example.js", and save it to the folder you want:

?

1

2

3

4

5var http = require (' http ');

Http.createserver (function (req, res) {

Res.writehead ({' Content-type ': ' Text/plain '});

Res.end (' Hello Node ');

}). Listen (1337, ' 127.0.0.1 ');

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

?

1

2CD C:websitesnodetest

Node Example.js

Open your Web browser and navigate to input http://127.0.0.1:1337. Does it work? Congratulations on your running your first Node.js application!

Provide Web services

Would you worry that I would just leave a "Hello world" sample to wrap up? It would be even better if we knew how to run an HTML file. Add a "index.html" file, which can be any HTML content. will be like this

?
1 2 3 4 5 6 7 8

It's time to run the application. Create a new file that can be called 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, ' 127.0.0.1 ');

Things are starting to get more interesting here. Note that the beginning of the place more than a line of "require." You are bringing in your application the required dependency program. This is like a "using" namespace directive used in C # to invoke a dependent program.

Run "Index.js" by typing in PowerShell: Node Index.js (don't forget to tap Ctrl-c to exit the last node application, or use a new port number at this time). In your browser, navigate to http://127.0.0.1:1337 and you should see your HTML file. You will probably feel a little bit excited about this achievement, but if you want me, you will have some complex feelings about it. This is just low-level programming, and if I have to think about reading/streaming media files and the questions about what state to send every time, the world will soon be in trouble. Go to Expressjs Road!

Using the node Package Manager

Node.js has a partner who once again makes us feel better around the world. Expressjs masks the old tune that needs to be done in node.js, allowing you to go directly to web development. It's a web framework that lets you build a single page, multi-page, and mixed-type Web application. Without it you will not have a node.js in the field of the world!

First use NPM to install it. To do this, open the PowerShell again and switch to the path to your application. Now enter: NPM Install Express. It will create a expressjs called "Node_modules" to install. From this point of view, your node module will be put there, a bit like. NET application, where you can call or "require" your dependency program.

Getting Started with Expressjs

Now create a new file, such as "server.js", and paste the following code in:

?
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 to App.listen (1337);

This is the invocation of EXPRESSJS dependencies, and then creates an application from it. You're going to be a big man! Here, we simply provide a static file service. "__dirname" is a special variable from the Expressjs, meaning the root file system location. Finally you tell the application to listen to port 1337. Now you have a Node.js site that provides static file services! In addition, add some HTML files, some in subdirectories, and then go to the http://127.0.0.1:1337 test to see.

About IIS

In these examples, I always run the application on port 1337, not port 80. The reason is that IIS is already listening on port 80. There are many ways to make IIS and Node.js coexist harmoniously:

Iisnode: This is a very smart idea to have node.js run like an application pool in your IIS site, just like running PHP in IIS. In fact, Azure is using this to run Node.js on its platform.

Winserv: It makes node.js run like a Windows service. It is actually a node.js friendly package for the popular NSSM (non-sucking Service Manager). Once you are running as a service, you can use the application request route (ARR) of IIS to broker a request to your Node.js application port.

About MS SQL

There are many MS SQL drivers prepared for node.js, some even cross-platform. One that can only be run in a Windows environment is published by Windows Azure: Microsoft Driver for node.js for SQL Server. And you can start working as follows:

?
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);D atabase=adventureworks2012; Trusted_connection={yes} ";   var cmd = "Select Top ten 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]);}});

Summarize

These are just fur! With Expressjs, you will be able to create fully fledged MVC applications with routes, views, layouts, services, and more components. Also, unless you need to integrate some existing Microsoft applications or MS SQL databases, MongoDB you create a node stack is a good partner to help you liberate yourself from SQL. Finally, you can use mean to create a mean JavaScript full stack application that includes MongoDB, Expressjs, Angularjs, and Node.js. Now that companies have moved closer to Node.js, is it also time for you to assist in the 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: info-contact@alibabacloud.com 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.