Node(23) Express

来源:互联网 发布:代办淘宝贷款骗局 编辑:程序博客网 时间:2024/06/05 20:52

Express is the most popular Node framework. Use Express to handle get and post are extremely simple.


Hello World

var app = require('express').createServer();app.get( '/',function( req, res ){console.log( req.header( 'host'));console.log( req.header( 'Referer'));res.end( 'hello world' );});app.listen( 9000 );console.log( 'server started' );


wildcard support

var express = require('express' );var app = express.createServer();app.get( '/:id?', function( req, res ){if( req.params.id ){res.send(req.params.id);}else{res.send('oh hai');}});app.listen(9000);


serve static content

//use static content from content folderapp.use(express.static(__dirname + '/content'));

get post data

app.use( express.bodyParser());app.post( '/postURL', function( request, response ){      var body = request.body;      //use body});




get and post:

http://www.hacksparrow.com/post-get-request-handling-in-node-js-express.html