Express.jsフレームワークでウェブアプリケーションを開発するサンプルです。
app.js
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('less-middleware')({ src: __dirname + '/public' }));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', function(request, response){
response.send("helo express new aaabbb-bobopapa.");
});
app.get('/hi', function(request, response){
response.send("<h1>hi</h1>");
});
app.get('/usrs/:userid', function(request, response){
response.send("<h1>Helo, User " + request.params.userid + "</h1>");
});
app.get('/home', function(request, response){
response.send(request.get('user-agent'));
});
app.param('from', function(request, response, next){
request.from = parseInt(request.params.from, 10);
next();
});
app.get('/user/:from-:to', function(request, response){
var from = request.from;
var to = parseInt(request.params.to, 10);
res.send("new.from=" + from + ", to=" + to);
});
var count = 0;
app.get('/helo.txt', function(request, response, next){
//res.send("app.js helo.txt");
count++;
next();
});
app.get('/count', function(request, response, next){
response.send(count + " views.");
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});