node. JS Three features: single-threaded, nonblocking I/O, event-driven
First, create a HelloWorld
1, the normal download installs the node. js package;
2. cmd command, enter the directory where node. JS Installer is located: Cd:c:\program Files\nodejs;
3, create JS file 01_helloworld.js, and write code:
var http=require ("http");//import HTTP Packet
Http.createserver (function (req,res) {//Create service
Res.writehead (200,{"ContentType": "Text/html;charset:utf-8"});//write response header, 200 indicates successful request
Res.end ("HelloWorld");
}). Listen ("4505", "127.0.0.1")//monitoring service
4. Enter the command in CMD node D:\NodeJS\nodedemo\01_helloworld.js
5. Enter the address in the browser http://127.0.0.1:4505/
6, "HelloWorld" will be presented
Note: In the JS file 01_helloworld.js writing code, there can be no space in front of VAR, otherwise there will be errors, and it will be difficult to find out why;
Second, create a static route
1. Create a folder and a static page under node setup;
2. The sample code is as follows:
Require means that the primer is a special feature that references itself
var http=require ("http");
var fs=require ("FS");
Http.createserver (function (req,res) {
if (req.url== "/index")
{
Fs.readfile ("./test/web/index.html", function (Err,data) {
if (ERR)
{
Console.log (ERR);
}
Req represents the request; Res represents the response, response
Set HTTP header, status code is 200, file type is HTML, character set is UTF8
Res.writehead (200,{"ContentType": "Text/html;charset=utf-8"});
Res.end (data);
})
}else if (req.url== "/map")
{
Fs.readfile ("./test/web/map.html", function (Err,data) {
if (ERR)
{
Console.log (ERR);
}
Res.writehead (200,{"ContentType": "Text/html;charset=utf-8"});
Res.end (data);
})
}else{
Res.writehead (200,{"ContentType": "Text/html;charset=utf-8"});
Res.end ("No this page");
}
}). Listen ("4050", "127.0.0.1");//running server, listening 4050 port (port number can be changed)
Third, the URL class
1. Sample code:
var http = require ("http");
var url = require ("url");
var server = Http.createserver (function (req,res) {
Url.parse () can divide a full URL into a number of parts:
Host, port, pathname, path, query
var pathname = Url.parse (req.url). Pathname;
Url.parse () If the second argument is true, then all queries can be changed to object
You can just get this parameter straight.
var query = Url.parse (req.url,true). query;
Get this parameter directly
var age = Query.age;
Console.log ("pathname:" + pathname);
Console.log ("query:" + query);
Console.log ("Age:" + age);
Res.end ();
});
Server.listen (3000, "127.0.0.1");
Four, folder operation
1. Create a folder:
var http=require ("http");
var fs=require ("FS");
Http.createserver (function (req,res) {
if (req.url== "/favicon.ico")
{
Return
}
Fs.mkdir ("MM");//Create mm folder
}). Listen ("4050", "127.0.0.1");
In the same vein there are other functions:
Stat Detection Status
Fs.stat ("./album/bbb", function (Err,data) {
Detects this path, is not a folder
Console.log (Data.isdirectory ());
});
Read all contents in a folder
Fs.readdir ("./test", function (Err,files) {});
V. Path class:
1. Primer Package:
var path=require ("path");
2. Function:
var pathname=url.parse (req.url). Pathname;
var extname=path.extname (pathname);//Get extension
3. Sample code (example of a function to complete a simple route):
var http = require ("http");
var url = require ("url");
var fs = require ("FS");
var path = require ("path");
Http.createserver (function (req,res) {
Get the user's path
var pathname = Url.parse (req.url). Pathname;
Default Home
if (pathname = = "/") {
pathname = "index.html";
}
Extended Name
var extname = path.extname (pathname);
Really read this file
Fs.readfile ("./static/" + pathname,function (err,data) {
if (err) {
If this file does not exist, it should be returned with 404
Fs.readfile ("./static/404.html", function (Err,data) {
Res.writehead (404,{"Content-type": "Text/html;charset=utf8"});
Res.end (data);
});
Return
};
MIME type, which is
Web file: text/html
JPG files: image/jpg
var mime = getmime (extname);
Res.writehead (200,{"Content-type": MIME});
Res.end (data);
});
}). Listen (3000, "127.0.0.1");
function Getmime (extname) {
Switch (extname) {
Case ". html":
return "text/html";
Break
Case ". jpg":
return "Image/jpg";
Break
Case ". css":
return "TEXT/CSS";
Break
}
}
Vi. Summary:
node. JS Three features: single-threaded, nonblocking I/O, event-driven
node. JS Beginner First Day