node基本知识

创建一个node的基本服务

// 引入http
var http=require('http');
// 创建服务中间层
var server=http.createServer((req,res)=>{
    // 请求头重写  // 重写请求头(content-Type 参考http://tool.oschina.net/commons)
    res.writeHead(200,{ 'Content-Type': 'text/html;charset=UTF8' })
    // res.end('我是一个node服务')// 文本
    res.end("<h1 style='color:#f00'>我是代码h1</h1>") //标签
})

server.listen(3000,'127.0.0.1')
console.log('http://127.0.0.1:3000/')

创建一个node基本路由

var http = require("http");
// req 路由监听空 res 上下文函数
var server = http.createServer((req, res) => {
  // 公共请求头
  res.writeHead(200, { "Content-Type": "text/html;chaset=UTF-8" });
  // 路由监控
  if (req.url == "/fang") {
    res.end("fang");
  } else if (req.url == "/yuan") {
    res.end("yuan");
  } else {
    // res.end("404");
    res.end("<a href='/fang'>fang</a><br><a href='yuan'>yuan</a>")
  }
});
server.listen(3000,"127.0.0.1", () => {
  console.log("http://127.0.0.1:3000/");
});

node文件读取

var http = require("http");
var fs = require("fs");
var server = http.createServer((req, res) => {
  if (req.url == "/yuan") {
    // res.end("fang");
    fs.readFile("./yuan.html", function (err, data) { //读取fang.html 的文件包括 ./fang/fang.css ./fang/1.jpg等资源
      if (err) {
        console.log(err, "fang");
        return;
      } else {
        res.writeHead(200, { "Content-Type": "text/html;charset:UTF-8" });
        // data表示二进制数据
        res.end(data);
      }
    });
  } 
  })
  
server.listen(3000, "127.0.0.1", () => {
  console.log("http://127.0.0.1:3000");
});

yuan.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <h1>我还yuan</h1>
</body>

</html>

common.js 模块导出

es5
var es5 = 'es5';
var fn = function () {
    console.log(es5)
}
exports.es5 = es5;
exports.fn = fn;
//--------------------------------------
var foo = require('./es5/1.js');
console.log(foo.es5);
foo.fn();
//======================================
function Person(name, age) {
    this.name = name;
    this.age = age;

}

/*设置原型*/
Person.prototype = {

    say: function () {
        console.log(this.name);

    }
}
/*对于类的暴露我们不能直接使用exports
* 我们需要使用module.exports*/

// exports.Person = Person;
module.exports = Person;
//--------------------------------------
var Person = require('./es5/2.js');

var person1 = new Person('xiaoming',12);
console.log(person1.name);
//========================================

前端es6

export const msg='zs';
export let fn=function(){
    console.log(1)
}
//------------------------------------
import { msg, fn } from './es5/3.js'
console.log(msg)
fn()
//========================================
export default  function() {
    console.log('es6')
}
//--------------------------------------
import fnu from './4es6/2';
fnu()
//===================================
var formatTime = function () {
    console.log('formatTime')
}
var search = function () {
    console.log(search)
}
var gets = function () {
    console.log(gets)
}
export {
    formatTime,
    search,
    gets
}
export default {
    formatTime,
    search,
    gets
}
//------------------------------------
import obj from './4es6/1'
obj.gets()

//====================================
//小程序
var formatTime = function () {
    console.log('formatTime')
}
var search = function () {
    console.log(search)
}
var gets = function () {
    console.log(gets)
}

module.exports = {
    formatTime: formatTime,
    search: search,
    gets: gets
}
//----------------------------------------
import {
    formatTime,
    search,
    gets
} from './es5/4.js';
//========================================
export let str='str'
export function fn(){
    console.log(str,'str')
}
//--------------------------------------
import * as fn from './es6/3.js'

console.log(fn)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

web修理工

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

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

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

打赏作者

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

抵扣说明:

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

余额充值