HTTP构建一个网站:
var http = require('http');var fs = require('fs');var server = http.createServer(function(req, res) {if('GET' === req.method && '/img' === req.url.substr(0,4) && '.jpg' === req.url.substr(-4)) {fs.stat(__dirname + req.url, function(err, stat) { //检查文件是否存在if(err || !stat.isFile()) {res.writeHead(404);res.end('Not Found');return;}serve(__dirname + req.url, 'image/png'); //'application/jpg':会下载图片});} else if('GET' === req.method && '/' === req.url) {serve(__dirname + '/index.html','text/html');} else {res.writeHead(400);res.end('Not Found');}function serve(path, type) {res.writeHead(200, {'Content-Type': type});fs.createReadStream(path).pipe(res); //将文件系统流接到HTTP响应流中;}});server.listen(3000);
通过connect实现一个网站:
{"name": "my-website","version": "0.0.1","dependencies": {"connect": "1.8.7"},"description": "an connect website"}
//用connect修改http构建的网站;var connect = require('connect');var server = connect.createServer(); //可以简化 server = connect();server.use(function(req, res, next) {console.log(' %s: %s ', req.method, req.url); next(); //下一个中间件;});server.use(function(req, res, next) {if('GET' === req.method && '/img' === req.url.substr(0,4)) {console.log('img');} else {next();}});server.use(function(req, res, next) {if('GET' === req.method && '/' === req.url) {console.log('index');} else {console.log('other');next();}});server.use(function(req, res, next) {res.writeHead(404);res.end('Not Found');});server.listen(3000);
中间件://中间件由函数组成,它除了处理req,res对象外还接受 一个next函数做流控制;
//sample.js/*请求超时*/module.exports = function(opts) {var time = opts.time || 100;return function(req, res, next) {console.log(1);var timer = setTimeout(function() {console.log('/033[90m%s %s/033[91mis taking too long!/033[39m', req.method, req.url);}, time); var end = res.end; res.end = function(chunk, encoding) { res.end = end; res.end(chunk, encoding); clearTimeout(); } next(); }}//main.jsvar connect = require('connect');var time = require('./3.2');var server = connect.createServer();/*记录请求情况*/server.use(connect.logger('dev'));/*实现时间中间件*/server.use(time({time: 500}));/*快速响应*/server.use(function(req, res, next) {if('/a' === req.url) {res.writeHead(200);res.end('Fast!');} else {next();}});/*慢速响应*/server.use(function(req, res, next) {if('/b' === req.url) {setTimeout(function() {res.writeHead(200);res.end('slow');}, 1000);} else {next();}});server.listen(3000);
server.use('/my-images', connect.static(__dirname + '/website/img'));
server.use('/js', connect.static(__dirname, {maxAge:10000}));
server.use(connect.static(__dirname, {hidden:true}));
server.use(connect.query())server.use(function (req, res) { console.log(req.query.name); //req.query对象格式返回,如访问xxx?name = jinks;返回{name:'jinks'}})
server.use(connect.bodyParser());server.use(function(req, res, next) { //req.body; }
var connect = require('connect');var fs = require('fs');var server = connect(connect.logger('dev'),connect.static('static'),connect.bodyParser()); //简化加载中间件//设置默认url:'/'为文件系统中static文件夹,如果有Index.html,就加载此项;server.use(function(req, res, next) {if('POST' === req.method ) {fs.readFile(req.files.files.path, 'utf8', function(err, data) {if(err) {res.writeHead(500);res.end('Error!');return;}res.writeHead(200, {'Content-Type': 'text/html'});res.end(['<h3>File: ' + req.files.files.name + '</h3>','<h4>Type: ' + req.files.files.type + '</h4>','<h1>Contents:</h4><pre>' + data + '</pre>'].join(''));});} else {next();}})server.listen(3000);
server.use(connect.cookieParser());server.use(function(req, res ,next) {//req.cookies //对象形式返回});
session中间件;
connect.session({secret: 'my app secret'})
创建会话:
--------------users.json----------------------------------//当只要对外暴露数据时,不需要module.exports而直接把数据文件以JSON形式暴露出来 {"jinks": {"passWord" : "ferret","name": "jinks"}}--------------app.js------------------------------------var connect = require('connect');var users = require('./users');var server = connect(connect.logger('dev'),connect.bodyParser(),connect.cookieParser(),connect.session({secret: 'my app secret'}), function(req, res, next) { console.log(req.session.logged_in);if('/' === req.url && req.session.logged_in) {res.writeHead(200, {'Content-Type': 'text/html'});res.end('welcome back, <b>' + req.session.name + '</b>. '+ '<a href="/logout">Logout</a>');} else {next();} },function(req, res, next) {if('/' === req.url && 'GET' === req.method) {res.writeHead(200, {'Content-Type': 'text/html'});res.end(['<form action="/login" method="post"' +'<fieldset>' +'<legend>Please log in</legend>' +'<p>User: <input type="text" name="user"/></p>' +'<p>Password: <input type="password" name="password"/></p>' +'<button>Submit</button>' +'</fieldset>' +'</form>'].join(''));} else {next();}},function(req, res, next) {if('/login' === req.url && 'POST' === req.method) {res.writeHead(200);if(!users[req.body.user] || req.body.password != users[req.body.user].password) {res.end('Bad username/password');} else {req.session.logged_in = true;req.session.name = users[req.body.user].name;res.end('Authenticated!');}} else {next();}},function(req, res, next) {if('/logout' === req.url) {req.session.logged_in = false;res.writeHead(200);res.end('Logged out!');} else {next();}});server.listen(3000);
新闻热点
疑难解答