Skip to content

Instantly share code, notes, and snippets.

@joao-neves95
Last active January 17, 2018 16:43
Show Gist options
  • Save joao-neves95/bfc92ff011843b446b09af324e4eb2a4 to your computer and use it in GitHub Desktop.
Save joao-neves95/bfc92ff011843b446b09af324e4eb2a4 to your computer and use it in GitHub Desktop.
freeCodeCamp's "Basic Node and Express" myApp.js and package.json files, for when the BETA goes live.
'use strict'
const express = require('express')
const helmet = require('helmet')
const logger = require('morgan')
const compression = require('compression')
const path = require('path')
const bodyParser = require('body-parser')
const app = express();
app.use(helmet())
app.disable('x-powered-by')
app.use(logger('dev'))
app.use(compression())
// --> 7) Mount the Logger middleware here
// app.use((req, res, next) => {
// console.log(req.method + ' ' + req.path + ' ' + '-' + ' ' + req.ip)
// next()
// })
// --> 11) Mount the body-parser middleware here
app.use(bodyParser.urlencoded({extended: false}))
/** 1) Meet the node console. */
// app.use('*', (req, res, next) => {
// console.log('Hello Express')
// next()
// })
/** 2) A first working Express Server */
// app.get('/', (req, res) => {
// res.status(200).send('Hello Express')
// })
/** 3) Serve an HTML file */
app.get('/', (req, res) => {
res.status(200).sendFile(path.join(__dirname, 'views', 'index.html'))
})
/** 4) Serve static assets */
app.use('/', express.static(path.join(__dirname, 'public')))
// /** 5) serve JSON on a specific route */
app.get('/json', (req, res) => {
let message = 'Hello json'
if (process.env.MESSAGE_STYLE === 'uppercase') {
return res.status(200).json({"message": message.toUpperCase()})
}
return res.status(200).json({"message": message})
})
/** 6) Use the .env file to configure the app */
/** 7) Root-level Middleware - A logger */
// place it before all the routes !
/** 8) Chaining middleware. A Time server */
app.get('/now', (req, res, next) => {
req.time = new Date().toISOString()
next()
}, (req, res) => {
res.status(200).json({"time": req.time})
})
/** 9) Get input from client - Route parameters */
app.get('/:word/echo', (req, res) => {
res.status(200).json({"echo": req.params.word})
})
/** 10) Get input from client - Query parameters */
// /name?first=<firstname>&last=<lastname>
// app.route('/name').get((req, res) => {
// res.status(200).json({"name": req.query.first + ' ' + req.query.last})
// }).post((req, res) => {
// res.status(201).json({"name": req.query.first + ' ' + req.query.last})
// })
/** 11) Get ready for POST Requests - the `body-parser` */
// place it before all the routes !
/** 12) Get data form POST */
app.post('/name', (req, res) => {
res.status(201).json({"name": req.body.first + ' ' + req.body.last})
})
// This would be part of the basic setup of an Express app
// but to allow FCC to run tests, the server is already active
/** app.listen(process.env.PORT || 3000 ); */
//---------- DO NOT EDIT BELOW THIS LINE --------------------
module.exports = app;
{
"name": "fcc-learn-node-with-express",
"version": "0.1.0",
"dependencies": {
"express": "^4.16.2",
"body-parser": "^1.18.2",
"cookie-parser": "^1.4.3",
"fcc-express-bground": "https://github.com/Em-Ant/fcc-express-bground-pkg.git",
"compression": "^1.7.1",
"helmet": "^3.9.0"
},
"devDependencies": {
"morgan": "^1.9.0"
},
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"engines": {
"node": "4.4.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment