Node(23) Express

欧阳绪
2023-12-01

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 folder
app.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



 类似资料: