


A simple introductory tutorial for Node.js under Windows system_node.js
With Paypal and Netflix recently announcing their migration to Node.js, the server-side Javascript platform has proven its value in the enterprise field. This is a small step for Node, but a big leap for Javascript. ! Programmers from .NET, Java, PHP, Ruby on Rails and more technical fields, all server-side coders will converge on this platform. As big players like Yahoo, Walmart, and Oracle enter the game, , Node is shedding its bad reputation of being immature and unstable. In this article, I will show you how easy it is to install Node.js on Windows.
Install Node.js
Getting Node.js installed on Windows is a piece of cake. Go to the Node.js website, download and run the ".msi" file. It will install Node.js and NPM (Node Package Management Module). NPM is equivalent For the NuGet package manager for .NET applications.
Run Node.js
Running Node.js on Windows is equally easy. Open PowerShell and type "node -v" Make sure Node is in your environment variables and see what version of Node.js you are running. Likewise type "npm -v" " Let’s check the version of the Node package management tool you installed. Are you done? Ok, let’s start having fun!!
Open the Notepad program, we will build our first Node.js application. Copy the following code into the Notepad program, use any file name, such as "example.js", and save it Go 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 where your "example.js" file is and run Node!
cd C:\Websites\NodeTest node example.js
Open your web browser and navigate to http://127.0.0.1:1337. Did it work? Congratulations on running your first Node.js application!
Providing website services
Are you worried that I will just leave a "Hello World" example and call it a day? 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 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 app. Create a new file with 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, '127.0.0.1');
Things start to get more interesting here. Notice the extra line "require" at the beginning. You are bringing in the required dependencies into your application. This is similar to the "require" line used in C# to call dependencies. using" namespace directive.
Run "index.js" by typing: node index.js in PowerShell (don't forget to hit Ctrl-C to exit the last Node application run, 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. You will probably be a little excited at this achievement, but if you miss me, just I'm going to have some mixed feelings about it. This is still low-level programming, and the world would change quickly if I had to think about reading/streaming files and what status should be sent each time. I have a lot of troubles. Say hello to ExpressJS!
Use the Node package manager
Node.js has a partner that makes the world feel beautiful again. ExpressJS blocks the need to repeat the same old tricks in Node.js, allowing you to directly enter web development. It is a tool that allows you to build single pages, A web framework for multi-page and mixed-type web applications. Without it you have no hope in the Node.js world!
First install it using NPM. To do this, open PowerShell again and switch to the path of your application. Now type: npm install express. It will create a node called "node_modules" to install ExpressJS. From this perspective As you can see, your Node modules will be placed there, kind of like the "bin" directory in a .NET application, and from here you can call or "require" your dependent programs.
Getting Started with ExpressJS
Now create a new file, such as "server.js", and paste the following code into it:
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);
这是在调用ExpressJS的依赖, 然后从它那里创建一个应用. 从此你可就牛逼大发了! 在这里,我们只是简单的提供静态文件服务. "__dirname" 是来自ExpressJS的一个特殊的变量,意思是根文件系统位置. 最后你告诉应用去侦听端口 1337. 现在你就拥有了一个提供静态文件服务的 Node.js 站点了! 另外在新增一些HTML文件,一些放在子目录中,然后到http://127.0.0.1:1337 测试看看吧.
关于 IIS
在这些示例中, 我一直都是在端口1337运行应用,而不是端口80.原因是IIS已经侦听了80端口. 有许多的方法可以使IIS 和 Node.js 和谐共存:
- IISNode: 这是一个在你的IIS站点让Node.js像一个应用池那样运行的很聪明的点子, 同在IIS中与运行PHP很像. 事实上,Azure就是用这个在其平台上运行Node.js的.
- WinServ: 它让 Node.js 像一个Windows服务那样运行. 它实际上是对流行了 NSSM (Non-Sucking Service Manager)的一个对Node.js友好的封装. 一旦作为一个服务运行,你就可以使用IIS的应用请求路由(ARR) 来代理向你的Node.js应用端口发起的请求.
关于 MS SQL
有许多为Node.js准备的 MS SQL 驱动程序, 有些甚至是跨平台的. 有一个只能在Windows环境中运行的,是由Windows Azure发布: Microsoft Driver for Node.js for SQL Server. 而你可以像下面这样开始工作:
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]); } }); });
总结
这些都只是皮毛! 与 ExpressJS携手, 你将能够创建带有路由、视图、布局、服务还有更多组件的完全成熟的MVC应用程序. 同样,除非你需要去集成一些现有的Microsoft应用程序或者MS SQL数据库, MongoDB 在你创建一个Node堆栈式是能帮助你从SQL中解放的好伙伴. 最后,你可以使用MEAN创建一个MEAN Javascript全栈应用, 包括有MongoDB, ExpressJS, AngularJS, 和Node.js. 现在企业已经向Node.js靠拢了, 对你而言同样是不是时候来辅助行动了呢?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

The open source VNC tool Tigervnc is compatible with a wide range of operating systems, including Windows, Linux, and macOS. This article will introduce in detail the application of Tigervnc on the Debian system. Tigervnc is integrated in the application system of Debian system: In the Debian system, Tigervnc is integrated into the system as a VNC server component. Users can start VNC services through command line tools such as vncserver and customize display settings such as resolution and color depth. Cross-platform connection: Tigervnc client supports Windows, Linux, and macOS, which means users can run this from any

In Laravel development, dealing with complex model relationships has always been a challenge, especially when it comes to multi-level BelongsToThrough relationships. Recently, I encountered this problem in a project dealing with a multi-level model relationship, where traditional HasManyThrough relationships fail to meet the needs, resulting in data queries becoming complex and inefficient. After some exploration, I found the library staudenmeir/belongs-to-through, which easily installed and solved my troubles through Composer.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Mac system maintenance includes: disk management (use OmniDiskSweeper to clean disk space, use disk tools to check disk errors) memory management (use Activity Monitor to monitor memory usage, end over-occupying processes) startup item management (use Linc or LaunchControl to manage startup items, disable unnecessary startup items) system cache cleaning (use CleanMyMac X or manually clean system cache) software update (timely update system and applications) regular backup (use Time Machine to backup data regularly) good usage habits (not installing applications excessively, cleaning files regularly, and monitoring system logs)

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)
