index.js
-
model[pathname](req,res)
; - 相当于
app[login](req,res)
- 或者
app.login(req,res)
var http = require('http');
var url = require('url');
var model = require('./model/model.js');
http.createServer(function(req,res){
res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
var pathname = url.parse(req.url).pathname.replace('/','');
if(pathname != 'favicon.ico'){
try{
model[pathname](req,res);
// model.pathname(req,res);
}catch(err){
model['home'](req,res);
// model.home(req,res);
}
}
}).listen(8000)
module.js
var ejs = require('ejs');
var fs = require('fs');
var app = {
login:function(req,res){
ejs.renderFile('views/form.ejs',{},function(err,data){
res.end(data)
})
},
dologin:function(req,res){
var postStr = '';
req.on('data',function(chunk){
postStr += chunk;
})
req.on('end',function(err,chunk){
fs.appendFile('login.txt',postStr+'\n',function(err){
if(err){
console.log(err);
return;
}
console.log('写入数据成功');
})
res.end("<script>alert('登录成功');history.back()</script>")
})
},
register:function(req,res){
console.log('register');
res.end('register')
},
home:function(req,res){
console.log('home');
res.end('home')
},
}
module.exports = app;