Last active
December 23, 2016 16:53
-
-
Save saggie/fa97c7144bf579160ce82ba3d2cab5ab to your computer and use it in GitHub Desktop.
An example of a simple web server using Node and Express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var express = require('express'); | |
var routes = require('./routes'); | |
var app = express(); | |
var server = http.createServer(app); | |
app.configure(function() { | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.static(__dirname + '/public')); | |
app.use(express.logger()); | |
app.use(express.errorHandler({ | |
dumpExceptions: true, | |
showStack: true | |
})); | |
app.use(app.router); | |
}); | |
routes.configRoutes(app, server); | |
server.listen(3000); | |
console.log( | |
'Express server listening on port %d in %s mode', | |
server.address().port, | |
app.settings.env | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "exmaple", | |
"version": "0.0.0", | |
"private": true, | |
"dependencies": { | |
"express": "3.2.x" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var configRoutes = function(app, server) { | |
app.all('/api/:obj_type/*?', function(request, response, next) { | |
response.contentType('json'); | |
next(); | |
}); | |
app.get('/api/:obj_type/list', function(request, response) { | |
response.send({ title: request.params.obj_type + ' list' }); | |
}); | |
}; | |
module.exports = { configRoutes: configRoutes }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: