node 노트 (8) - Nodejs 루트 패키지 모듈 화 방식 패키지
3624 단어 데이터 뱅 크
var url = require('url');
function changeRes(res){
res.send= function(data){
res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});
res.end(data);
}
}
var Server = function(){
var G = this;
this._get = {};
this._post = {};
var app = function(req,res){
changeRes(res);
var pathname = url.parse(req.url).pathname;
if(!pathname.endsWith('/')){
pathname = pathname + '/';
}
var method = req.method.toLowerCase();
// if(G['_'+ method][pathname]){
if(G['_'+ method][pathname]){
if(method == 'post'){
var postStr = '';
req.on('data',function(chunk){
postStr += chunk;
})
req.on('end',function(err,chunk){
req.body = postStr;
G['_'+method][pathname](req,res);
})
}else{
G['_'+method][pathname](req,res);
}
}else{
res.end('no router');
}
}
app.get = function(string,callback){
if(!string.endsWith('/')){
string = string+'/'
}
if(!string.startsWith('/')){
string = '/'+ string;
}
G._get[string] = callback;
}
app.post = function(string,callback){
if(!string.endsWith('/')){
string = string+'/'
}
if(!string.startsWith('/')){
string = '/'+ string;
}
G._post[string] = callback;
}
return app;
}
module.exports = Server();
index2.js
var http = require('http');
var ejs = require('ejs');
var app = require('./model/express-route');
console.log(app);
http.createServer(app).listen(3000);
app.get('/',function(req,res){
var msg = ' ';
ejs.renderFile('views/index.ejs',{msg:msg},function(err,data){
res.send(data);
})
})
app.get('/login',function(req,res){
ejs.renderFile('views/form.ejs',{},function(err,data){
res.send(data);
})
})
app.post('/dologin',function(req,res){
console.log(req.body);
res.send("alert(' ');history.back(); ");
})
app.get('/register',function(req,res){
console.log('register')
res.send('register');
})
app.get('/news',function(req,res){
console.log('news')
res.send('news');
})
해석 하 다.
에 해당 하 다,...
http.createServer(function(req,res){
changeRes(res);
var pathname = url.parse(req.url).pathname;
if(!pathname.endsWith('/')){
pathname = pathname + '/';
}
var method = req.method.toLowerCase();
// if(G['_'+ method][pathname]){
if(G['_'+ method][pathname]){
if(method == 'post'){
var postStr = '';
req.on('data',function(chunk){
postStr += chunk;
})
req.on('end',function(err,chunk){
req.body = postStr;
G['_'+method][pathname](req,res);
})
}else{
G['_'+method][pathname](req,res);
}
}else{
res.end('no router');
}
}).listen(3000);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
nginx websocket ip_해시 규칙프로젝트 를 다운로드 한 후 서로 다른 네트워크 에 각각 이 demo 프로젝트 를 배치 합 니 다. 프로젝트 에서 환경 변수 에 따라 시스템 변 수 를 설정 합 니 다. spring.profiles.active=de...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.