current position:Home>Node. JS basic tutorial

Node. JS basic tutorial

2022-04-29 13:33:47Dada 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

Random recommended