node 创建

下载 node : 注意选项 ADD PATH 配置环境变量
node -v npm -v 检查
创建一个服务器 : //创建一个服务器 127.0.0.1:8888

var http = require("http");
http.createServer(function(req,res){
res.writeHead(200,{"Content-type":"text/blain"});
res.write("Hello NodeJs\n");
res.end();
}).listen(8888,"127.0.0.1");
console.log("server pis running at http://127.0.0.1:8888/");
----------------------------------------------------------------------------------------------------------------------------- var express = require("express");
var path = require('path');
var app = express();
app.get('/',function (req,res) {
res.send('Hello World');
})
app.get('/goods',function (req,res) {
res.send('Hello goods');
})
app.use(express.static(path.join(__dirname, 'dist')))
// app.use(express.static('public')); 
//__dirname 当前文件路径(绝对地址)
app.get('/index', function (req,res) {
res.sendFile(__dirname+'/index.html'); //http://127.0.0.1:8081/index
})
var server = app.listen(8081,function(){
var host=server.address().address
var port = server.address().port
console.log("应用实例,访问地址为 http://%s:%s", host, port)
})

启动服务 : win+r 
C:\Users\Administrator\Desktop\tiger\node 
node server.js //通过node启动server.js
重启服务: ctrl+c 

node 导入导出小例子 : 
//学生类
function add(student){
console.log("最不听话的学生 ---:"+ student)
}
exports.add = add    //exports.add 存

//老师类
function add(teacher){
console.log("学前教育班主任---:"+teacher)
}
exports.add = add    

//班级类
var student = require('./student') //require取
var teacher = require('./teacher')

function add(teacherName,students){
teacher.add(teacherName)
students.forEach(function(item,index){
student.add(item)
})
}
exports.add =add // module.exports=add 原型链上

//入口
var klass = require('./klass') 
klass.add('李涓兰',['杨日燃','覃靖源'])

 

posted @ 2019-04-24 15:14  八十易  阅读(212)  评论(0编辑  收藏  举报