Created
May 28, 2014 10:19
-
-
Save GuillermoPena/091869410c55a79d0212 to your computer and use it in GitHub Desktop.
NodeJS - EXPRESS : Simple Web Service
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
// How to create a simple web service with Express | |
// In App.js | |
// Dependencies | |
var express = require('express') | |
, http = require('http') | |
, port = 8080 | |
// Application | |
var app = express() | |
// Configuration: All environments | |
app.use(express.bodyParser()) // Parser | |
app.use(express.methodOverride()) | |
app.use(app.router) // Router | |
// Configuration: Development only | |
app.configure('development', function() { | |
app.use(express.errorHandler()) | |
}) | |
//Routes are in a separate file | |
routes = require("./express.routes/router")(app) | |
//Start the server | |
http.createServer(app).listen(port) | |
//**********************************************************************************// | |
// In "./express.routes/router.js" | |
//App routes | |
module.exports = function(app){ | |
var sayHello = function(req, res) { | |
res.send("Hello " + req.params.id) | |
} | |
// Link routes and functions | |
// Functions can be in others files | |
app.get('/hello', sayHello) | |
app.post('/hello', sayHello) | |
app.get('/hello/:id', sayHello) | |
app.post('/hello/:id', sayHello) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment