Last active
August 29, 2015 14:08
-
-
Save dasniko/eaca57114ac70331ed54 to your computer and use it in GitHub Desktop.
Simple Node.js/Express app
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 express = require('express') | |
var app = express() | |
// respond with "Hello World!" on the homepage | |
app.get('/', function (req, res) { | |
res.send('<h1>Hello JavaScript!</h1>'); | |
}); | |
// accept POST request on the homepage | |
app.post('/', function (req, res) { | |
res.send('Got a POST request</h1>'); | |
}); | |
// accept GET request at /user | |
app.get('/user', function (req, res) { | |
res.send('<h1>Hello John Doe!</h1>'); | |
}); | |
// accept PUT request at /user | |
app.put('/user', function (req, res) { | |
res.send('Got a PUT request at /user'); | |
}); | |
// accept DELETE request at /user | |
app.delete('/user', function (req, res) { | |
res.send('Got a DELETE request at /user'); | |
}); | |
var server = app.listen(3000, function () { | |
var host = server.address().address; | |
var port = server.address().port; | |
console.log('Example app listening at http://%s:%s', host, port); | |
}); | |
/* | |
var http = require('http'); | |
http.createServer(function (req, res) { | |
res.writeHead(200, { | |
"Content-Type" : "text/html" | |
}); | |
res.end("<h1>Hello JavaScript!</h1>"); | |
}).listen(8000); | |
console.log("Server running at http://127.0.0.1:8000/"); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment