node.js基础

node.js基础

对象

let obj={
    a : 1,
    b : {
        c:'hello',
        d:true
    }
};
console.log(obj.b.c);
//输出:hello
let arr=[1,2,3];
console.log(arr.length);
//输出:3
console.log(arr.map(v=>v*2));
//输出:[2,4,6]

变量定义

// var定义的变量可以重复定义
var x=0;
var x=1;

// let定义的变量只允许定义一次
let x=0;
let x=1;// 报错:SyntaxError: Identifier 'x' has already been declared

// const定义的变量不可重新赋值
const x=1;
x=2;// 报错:TypeError: Assignment to constant variable.

//不允许换行,使用‘+’拼接字符串
var user='jishengming';
var x='hello-'+user;
console.log(x);// 输出:hello-jishengming

条件语句

const b='1';
if(b==1){
    console.log('hello');
}// 输出:hello

switch(b){
    case '1':
        console.log('1的情况');
        break;
}// 输出:1的情况

console.log(b && 2);// 输出:2

console.log(b || 2);// 输出:1

循环

for(let i=0;i<10;i++)
{
    console.log(i);
}

let i=0;
while(i<10){
    console.log(i);
    i++;
}

let i=0;
do{
    console.log(i);
    i++;
}while(i<10)

函数

function hello(){
    console.log('hello');
}
hello();// 输出:hello

function hello(name){
    console.log(`hello ${name}`);// 注意符号,不是单引号
}
hello('Ji Shengming');// 输出:hello Ji Shengming

function hello(name = 'Ji Shengming'){
    console.log(`hello ${name}`);// 注意符号,不是单引号
}
hello();// 输出:hello Ji Shengming

异步

// callback 异步回调
setTimeout(function(){
    console.log('world');
},1000);
console.log('hello');// hello输出1000ms后输出world

setTimeout(function(){
    console.log(1);
    setTimeout(function(){
        console.log(2);
    },1000);  
},1000);// 1000ms后输出1,再1000ms后输出2

//promise
const promise1=new Promise(function(resolve,reject){// 调用函数,两个参数分别表示成功,失败
    setTimeout(function(){resolve('foo')},300);
});
promise1.then((value)=>{
    console.log(value);// 输出:foo
});
console.log(promise1);// 输出:Promise
//先输出promise1,300ms后再输出'foo'

//settimeout
async function timeout(time){
    return new Promise(function(resolve){
        setTimeout(resolve,time);
    });
}
await time(1000);
console.log(1);
await time(1000);
console.log(2);

模块

const http=require('http');
http.createServer((req,res)=>{
    res.writeHead(200,{'Content-Type':'text/html'});
    res.write('hello world\n');
    res.end();
}).listen(1337);
console.log('服务器运行中。。。');
//打开网址(http://localhost:1337/)即可看到输出的内容:hello world

-内置模块:编译进 Node 中,例如 http fs net process path 等
-⽂件模块:原⽣模块之外的模块,和⽂件(夹)⼀⼀对应

GET请求内容

var http=require('http' );
var url=require('url');
var util=require( "util");
http.createServer(function (request, response){
response.writeHead(200, {'Content-Type': 'text/html;charset="utf-8"'});
var params=url. parse( request.url, true).query;
response.write("学校= : "+params.name);
response.write("<br>");
response.write("学号= : "+params.id);
response.write("<br>");
response.write("姓名= : "+params.username);
response.end();
}).listen(8080);
console.log( 'Server running at http://127.0.0.1:8080/');

1

获取 POST 请求内容

var http = require('http');
var querystring = require('querystring');
 
var postHTML = 
  '<html><head><meta charset="utf-8"><title>菜鸟教程 Node.js 实例</title></head>' +
  '<body>' +
  '<form method="post">' +
  '网站名: <input name="name"><br>' +
  '网站 URL: <input name="url"><br>' +
  '<input type="submit">' +
  '</form>' +
  '</body></html>';
 
http.createServer(function (req, res) {
  var body = "";
  req.on('data', function (chunk) {
    body += chunk;
  });
  req.on('end', function () {
    // 解析参数
    body = querystring.parse(body);
    // 设置响应头部信息及编码
    res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'});
 
    if(body.name && body.url) { // 输出提交的数据
        res.write("网站名:" + body.name);
        res.write("<br>");
        res.write("网站 URL:" + body.url);
    } else {  // 输出表单
        res.write(postHTML);
    }
    res.end();
  });
}).listen(3001);
console.log( 'Server running at http://127.0.0.1:3001/');

2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是柒浔啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值