-
-
Save ruyaoyao/2d498c95189f49db9f432d8424282185 to your computer and use it in GitHub Desktop.
Express App Translated to ES2015
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
{ | |
"parser": "babel-eslint", | |
"env": { | |
"node": true, | |
"es6": true | |
}, | |
"rules":{ | |
"vars-on-top": 2, | |
"no-undef": 2 | |
} | |
} |
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
"use strict"; | |
import express from 'express'; | |
import path from 'path'; | |
import favicon from 'serve-favicon'; | |
import logger from 'morgan'; | |
import cookieParser from 'cookie-parser'; | |
import bodyParser from 'body-parser'; | |
import routes from './routes/index'; | |
import users from './routes/users' | |
//using let | |
let app = express(); | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', 'jade'); | |
app.use(logger('dev')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(cookieParser()); | |
app.use(express.static(path.join(__dirname, 'public'))); | |
app.use('/', routes); | |
app.use('/users', users); | |
// using arrow syntax | |
app.use((req, res, next) => { | |
let err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
if (app.get('env') === 'development') { | |
app.use((err, req, res, next) => { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: err | |
}); | |
}); | |
} | |
app.use((err, req, res, next) => { | |
res.status(err.status || 500); | |
res.render('error', { | |
message: err.message, | |
error: {} | |
}); | |
}); | |
module.exports = app; |
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
{ | |
"name": "es6-express", | |
"version": "0.0.0", | |
"description": "ES6/ES2015 Express Example", | |
"repository": "n/a", | |
"license": "MIT", | |
"author": "Steven", | |
"scripts": { | |
"lint": "eslint app.js", | |
"start": "babel-node ./bin/www", | |
"auto-start": "nodemon --exec \"npm run lint && npm start\" --ignore public/js" | |
}, | |
"dependencies": { | |
"body-parser": "~1.13.2", | |
"cookie-parser": "~1.3.5", | |
"debug": "~2.2.0", | |
"express": "~4.13.1", | |
"jade": "~1.11.0", | |
"morgan": "~1.6.1", | |
"serve-favicon": "~2.3.0" | |
}, | |
"devDependencies": { | |
"babel": "^5.8.21", | |
"babel-eslint": "^4.0.10", | |
"eslint": "^1.1.0", | |
"nodemon": "^1.4.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment