Created
June 23, 2014 02:35
-
-
Save piyasde/2ea71ecdcbe8d22d9053 to your computer and use it in GitHub Desktop.
New features in express.js
This file contains hidden or 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 morgan = require('morgan'); | |
| var bodyParser = require('body-parser'); | |
| var methodOverride = require('method-override'); | |
| var app = express(); | |
| app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users | |
| app.use(morgan('dev')); // log every request to the console | |
| app.use(bodyParser()); // pull information from html in POST | |
| app.use(methodOverride()); // simulate DELETE and PUT | |
| //This is a get example | |
| app.get('/api', function (req, res) { | |
| res.send('Express 4 API is running'); | |
| }); | |
| app.get('/', function(req, res){ | |
| res.send('hello world'); | |
| }); | |
| //This is request parameter example | |
| app.get('/params', function (req, res) { | |
| console.log(req.param('value')); | |
| var param = req.param('value'); | |
| res.send('This is params example '+param); | |
| }); | |
| //This is request originalurl example | |
| app.get('/paramsoriginal', function (req, res) { | |
| console.log(req.originalUrl); | |
| var param = req.originalUrl; | |
| res.send('This is params example '+param); | |
| }); | |
| //Check if the request was issued with the "X-Requested-With" header field set to "XMLHttpRequest" (jQuery etc). req.xhr | |
| app.get('/paramsxhr', function (req, res) { | |
| console.log(req.xhr); | |
| var param = req.xhr; | |
| res.send('This is params example '+param); | |
| }); | |
| app.route('/students') | |
| .get(function(req, res) | |
| { | |
| console.log('the get method for student'); | |
| res.send('the get method for student'); | |
| }) | |
| .post(function(req, res) | |
| { | |
| console.log('the post method for student'); | |
| res.send('the post method for student'); | |
| }) | |
| var teacher = express.Router(); | |
| teacher.get('/', function(req, res) { | |
| res.send('im the teacher landing page!'); | |
| }); | |
| teacher.get('/findus', function(req, res) { | |
| res.send('Here you will find teachers'); | |
| }); | |
| // apply the routes to application | |
| app.use('/teachers', teacher); | |
| app.listen(8080); | |
| console.log('Magic happens on port 8080'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment