current position:Home>Node. JS basic tutorial
Node. JS basic tutorial
2022-04-29 13:33:47【Dada front end】
Node.js Launch , It not only automates more trivial and time-consuming work from the perspective of Engineering , It also breaks the language boundary of the front-end and back-end , Give Way JavaScript Run smoothly on the server side , This series of courses is designed to guide front-end development engineers , as well as Node.js Beginners enter this lively and vibrant new world .
What is? node.js
Is to write a high-performance server JavaScript tool kit
Single thread , asynchronous , Event driven
characteristic , fast , High memory consumption
node.js High performance , High development efficiency , Wide application
node.js Installation :
Download address :http://node.js.cn
Test the installation environment :
dos Next command line :npm
see npm Version of :npm -v
see node.js Version command line :node -v
node.js It's based on Chrome v8 Engine JavaScript Running environment ,Node.js An event driver is used , Non-blocking type i/o Model of , Make it lightweight and efficient .
node.js Package manager npm, Is the world's largest open source system .
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ // Clear section 2 This visit
console.log(' visit ');
response.write('hello,world');
response.end('hell, The world ');// If you don't write, you don't http End of agreement , But writing will generate two visits
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
var http = require('http');
var otherfun = require('./models/otherfuns.js');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ // Clear section 2 This visit
otherfun.controller(request,response);
otherfun.call(response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
//--- Ordinary function
function fun1(res){
res.write(" Hello , I am a fun1");
}
function controller(req,res){
//res.write(" send out ");
call('hello',req,res);
res.end("");
}
function call(res){
console.log('call');
}
module.exports = controller; // Only one function is supported
/*
// Supports multiple functions
module.exports={
getVisit:function(){
return visitnum++;
},
add:function(a,b){
return a+b;
}
}
*/
var http = require('http');
var otherfun = require("./models/otherfuns.js");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ // Clear section 2 This visit
//fun1(response);
//------- Call the corresponding function with a string ---
funname = 'fun3';
otherfun[funname](response);
//otherfun['fun3'](response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
module.exports = {
fun2:function(res) {
console.log(" I am a fun2");
},
fun3:function(res){
console.log(" I am a fun3");
res.write(" Hello ");
}
}
var http = require('http');
var otherfun = require("./models/otherfuns.js");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html'}
if(request.url !== "/favicon.icon") {
otherfun(response);
response.end('');
}
}).listen(8000);
console.log('server running at http://127.0.0.1:8000/');
function fun1(res){
console.log("fun1");
res.write("hello");
}
var http = require('http');
var otherfun = require('./models/otherfuns.js");
http.createServer(function (request, response) {
response.writeHead(200,{'Content-Type': 'text/html'});
if(request.url !== "/favicon.ico"){
//fun1(response);
otherfun(response);
response.end('');
}
}
}).listen(8000);
function fun1(ress){
console.log("fun1");
res.write("hello");
}
function fun2(res){
console.log(" I am a fun2");
res.write(" Hello ");
}
module.exports = fun2; // Only one function is supported
module.exports = {
// Supports multiple functions
fun2: function(res){
console.log(" I am a fun2");
res.write(" Hello ");
},
fun3: function(res){
console.log(" I am a fun3");
res.write(" Hello ");
}
}
if(request.url!=="/favicon.ico"){// Clear section 2 visit
otherfun.fun2(response);
otherfun.fun3(response);
response.end('');
}
}).listen(8000);
otherfun['fun2'](response);
otherfun['fun3'](response);
response.end('');
node.js Call module
function User(){
this.id;
this.name;
this.age;
this.enter=function(){
console.log(" Enter the library ");
}
}
Module call
var http = require('http');
// var User=require('./models/User');
var Teacher = require("./models/Teacher');
http.createServer(function (request,response) {
response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){// Clear section 2 This visit
teacher = new Teacher(1,' Xiaohong ',30);
teacher.teacher(response);
response.end("");
}
}).listen(8000);
function User(id,name,age) {
this.id=id;
this.name=name;
this.enter=function(){
console.log("haha");
}
}
module.exports = User;
// models/Teacher.js
var User = require('./User');
function Teacher(id,name,age) {
User.apply(this,[id,name,age]);
this.teach=function(res){
res.write(this.name+"dashu");
}
}
module.exports = Teacher;
var User = require('./User');
function Teacher(id, name, age) {
User.apply(this,[id,name,age]);
this.teach=function(res){
res.write(this.name+" lecture ");
}
}
module.exports = Teacher;
var http = require('http');
// var User = require("./models/User");
var Teacher = require("./models/Teacher");
http.createServer(function (request, response) {
response.writeHead(200,{'Content-Type':'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){
teacher = new Teacher(1,'dashu', 20);
teacher.enter();
teacher.teach(response);
response.end('');
}
}).listen(8000);
node.js route :
var http = require('http');
var url = require('url');
var router = require('/router');
http.createServer(function(request,response){
respnse.writeHead(200, {'Content-Type': 'text/html'; charset=utf-8"});
if(request.url!=="/favicon.ico"){
var pathname=url.parse(request.url).pathname;
// console.log(pathname);
pathname = pathname.replace(/\//, ");// Replace the previous one
// console.log(pathname);
router[pathname](request,response);
response.end(");
}
}).listen(8000);
module.exports={
login: function(req,res){
res.write(" I am a login");
}
zhuce: function(req,res){
res.write(" I'm registered ");
}
}
Read the file :
var http = require('http');
var optfile=require('./models/optfile');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){
console.log(' visit ');
response.write("hello");
optfile.readfile();
response.end('hello");
}
}).listen(8000);
var fs = require('fs');
module.exports = {
readfile:funciton(path){
fs.readFile(path, function(err,data){
if(err){
console.log(err);
}else{
console.log(data.toString());
}
});
console.log("dashu");
},
readfileSync:function(path){
var data=fs.readFileSync(path,'utf-8');
console.log(" Synchronization method ");
return data;
}
}
copyright notice
author[Dada front end],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291041433485.html
The sidebar is recommended
- How to create JavaScript custom events
- Is there any recommendation for wireless signal and wired signal power amplification circuit?
- Introduction to basic methods of math built-in objects in JavaScript
- 1000km pure electric endurance is meaningless? Then why does the car factory still try its best to extend the endurance?
- [let's implement a simple vite!] Chapter 4 - compiling single file components
- 3-11xss htmlspecialchars bypass demo
- How to conduct architecture design 𞓜 deeply reveal Alibaba cloud serverless kubernetes (2)
- Heapdump performance community special series 7: front door opener of large factory -- Application Practice of fluent
- 1、 HTML base tag
- Don't leave the crane unless necessary! A case of novel coronavirus nucleic acid positive was found in Heshan, Guangdong
guess what you like
[Architect (Part 20)] self defined template of scaffold and summary of phase I
How does JavaScript understand this algorithm
[live review] openharmony knowledge empowerment lesson 2, phase 5 - becoming a community talent
Understand the basic history and knowledge of HTTP
Influence of lazy loading of Web front-end training on Web Performance
Guangxi 2021 Guangxi landscape - blessing Lake Changshou Island
Responsive gulp Chinese network of web front end
Twaver-html5 basic learning (26) background
CSS learning notes [3] floating, not separated from document flow, inheritance and stacking
Webengine loading local html is invalid, and html is the dynamic address generated by JS, which can be solved
Random recommended
- Event handling of react
- Character coding knowledge that needs to be understood in front-end development
- 05. JavaScript operator
- 06. JavaScript statement
- Vue basics and some components
- Introduction to front-end Er excellent resources
- Node. Summary of methods of outputting console to command line in JS
- The beginning of passion, the ending of carelessness, python anti climbing, Jiageng, friends asked for MIHA's API and arranged y24 for him
- Technology sharing | test platform development - Vue router routing design for front-end development
- Node under windows JS installation detailed steps tutorial
- Layui built-in module (element common element operation)
- Excuse me, my eclipse Exe software has clearly opened to display the number of lines. Why is it useless
- It was revealed that Meila of Sea King 2 had less than ten minutes to appear, which was affected by negative news
- Vue element admin how to modify the height of component El tabs
- Bootstrap navigation bar drop-down menu does not take effect
- Vue Preview PDF file
- Geely joined the hybrid sedan market, and Toyota seemed even more hopeless
- Mustache template engine for exploring Vue source code
- Referer and referer policy and picture anti-theft chain
- Explain the "three handshakes" and "four waves" of TCP connection in detail
- Introduction to basic methods of math built-in objects in JavaScript
- JavaWeb Tomcat (III) httpservlet
- Vue Za wiper technical scheme
- Differences, advantages and disadvantages between HTTP negotiation cache Etag and last modified
- After taking tens of millions less, the management of Lenovo holdings took the initiative to reduce the salary by 70%
- What if Vue introduces this file and reports an error?
- Use Hikvision Web3 in vue3 2. Live broadcast without plug-in version (II)
- How to learn high-order function "zero basis must see"
- Detailed explanation of JS promise
- Cesium drawcommand [1] draw a triangle without talking about the earth
- The role of webpack cli in webpack packaging
- Action system of coco2d-x-html5
- Vxe table check box paging data memory selection problem
- [hand tear series] hand tear promise -- this article takes you to realize promise perfectly according to promise a + specification!
- QT use qdialog to realize interface mask (mask)
- Differences between JSP comments and HTML comments
- Bankless: Ethereum's data report and ecological highlights in the first quarter of 22 years
- Spring mvc07: Ajax research
- Understand the basic history and knowledge of HTTP
- Technology sharing | learning to do test platform development vuetify framework