Windows system Node. js simple tutorial

  • 2020-06-19 09:46:39
  • OfStack

With the recent announcement of Paypal and Netflix's migration to Node.js, the server-side Javascript platform has proven itself valuable in the enterprise space. From. NET, Java PHP, Ruby on Rails and more technology in the field of programmers, all in server-side coders will gather to the platform. As like Yahoo Walmart, and Oracle such big players into the bureau, Node is get rid of its 1 straight are immature and unstable bad reputation. In this article, I will show you the installation Node Window environment. js how easy it was.
Install Node js

Getting Node.js installed on Windows is a trivial matter. Go to Node.js to download and run the ".msi "file. It will install Node.js and NPM (Node package management module). NPM is equivalent to the NuGet package manager for.NET applications.

Run Node js

Running ES46en.js on Windows is also easy. Open PowerShell and type "ES49en-ES50en" to make sure Node is in your environment variable and view the version of ES52en.js you are running. Ok, let's start having fun!!

Open Notepad and we will build our first ES60en.js application. Copy the following code into Notepad using any file name, such as "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, '127.0.0.1');

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


cd C:\Websites\NodeTest
node example.js

Open your web browser and navigate to http://127.0.0.1:1337. Congratulations on running your first ES79en.js application!

Provide Website services

You might worry that I'll just leave one example of "Hello World" and call it a day. It would be even better if we knew how to run an HTML file. Add a "ES88en.html" file that can contain any HTML content. It would look like this:


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

Things get a little more interesting here. Note that there is an extra line of "require" at the beginning. You are inserting the dependencies into your application. This is like the "using" namespace directive in C# to call dependencies.

By typing in PowerShell: node ES111en. js to run "ES113en. js"(don't forget to hit ES115en-ES116en to log out of the last Node application, or use a new port number this time). In your browser, navigate to http://127.0.0.1:1337, and you should see your HTML file. This is just low-level programming, and the world gets pretty freaking out pretty quickly when I have to think about reading/streaming media files and what state to send each time.

Use the Node package manager

Node. js has a partner that once again makes the world feel good. ExpressJS blocks out all the old stuff that needs to be done on ES129en. js and lets you go straight to web development. It's an web framework that lets you build single-page, multi-page and mixed-type web applications.

First use NPM to install it. To do this, open PowerShell again and switch to the path of your application. Now enter: npm install express. It will create one called "node_modules" to install ExpressJS. From this point of view, your Node module will be placed there, somewhat like the "bin" directory in the.NET application, from which you can call or "require" your dependencies.

Introduction to ExpressJS

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


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 calling ExpressJS's dependency and creating an application from it. From there on, you're on fire! ___ is a special variable from ExpressJS, meaning the root filesystem location. Finally you tell the application to listen on port 1337. In addition, add 1 HTML file, 1 in subdirectory, and then go to http://127.0.0.1:1337 test to see.

About IIS

In these examples, I've 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 ES176en.js coexist harmoniously:

IISNode: This is a clever idea to get ES181en.js to run as an application pool on your ES1818en site, similar to running PHP on IIS. In fact, Azure used this to run ES186en.js on its platform. WinServ: It makes ES189en.js run like an Windows service. It is actually a friendly encapsulation of the popular NSSM (Non-ES194en Service Manager). Once run as a service, you can use IIS's application request routing (ARR) to proxy requests to your ES201en.js application port.

About MS SQL

There are many MS SQL drivers for ES208en.js, and some are even cross-platform. There is one that only runs in Windows and is published by Windows Azure:


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

conclusion

It's only skin deep! Working with ExpressJS, you will be able to create fully mature MVC applications with routing, views, layout, services, and more. Again, unless you need to integrate 1 existing Microsoft application or MS SQL database, MongoDB is a great partner to help you free yourself from SQL when you create 1 Node stack. You can use MEAN to create a full stack of MEAN Javascript applications, including MongoDB, ExpressJS, AngularJS, and ES244en. js.


Related articles: