创建一个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)