Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save GuillermoPena/091869410c55a79d0212 to your computer and use it in GitHub Desktop.
Save GuillermoPena/091869410c55a79d0212 to your computer and use it in GitHub Desktop.
NodeJS - EXPRESS : Simple Web Service
// 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