-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
80 additions
and
26 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Express Boilerplate | ||
|
||
## Set up | ||
|
||
4. `npm install` | ||
5. Rename `example.env` to `.env` and add secrets as needed | ||
6. Edit `package.json` project name | ||
|
||
## Usage | ||
|
||
* `npm run dev` - run with Nodemon | ||
* `npm test` - Mocha default | ||
* `npm test -- --watch` - Mocha in watch mode | ||
* `npm run deploy` - pushes to Heroku master |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
NODE_ENV=development | ||
PORT=8000 | ||
EXAMPLE="example-environmental-variable" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require('dotenv').config(); | ||
const express = require('express'); | ||
const morgan = require('morgan'); | ||
const cors = require('cors'); | ||
const helmet = require('helmet'); | ||
|
||
const { NODE_ENV } = require('./config'); | ||
|
||
const app = express(); | ||
|
||
const morganOption = (NODE_ENV === 'production') | ||
? 'tiny' | ||
: 'common'; | ||
|
||
app.use(morgan(morganOption)); | ||
app.use(cors()); | ||
app.use(helmet()); | ||
|
||
app.get('/', (req, res) => { | ||
res.send('Hello, world!'); | ||
}); | ||
|
||
app.use(function errorHandler(error, req, res, next) { | ||
let response; | ||
if (NODE_ENV === 'production') { | ||
response = { error: { message: 'server error ' }}; | ||
} else { | ||
console.error(error); | ||
response = { message: error.message, error }; | ||
} | ||
res.status(500).json(response); | ||
}); | ||
|
||
module.exports = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
PORT: process.env.PORT || 8000, | ||
NODE_ENV: process.env.NODE_ENV || 'development', | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const app = require('./app'); | ||
const { PORT } = require('./config'); | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server listening at http://localhost:${PORT}`); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const app = require('../src/app'); | ||
|
||
describe('App', () => { | ||
it('GET / responds with 200 containing "Hello, world!"', () => { | ||
return supertest(app) | ||
.get('/') | ||
.expect(200, 'Hello, world!'); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { expect } = require('chai'); | ||
const supertest = require('supertest'); | ||
|
||
global.expect = expect; | ||
global.supertest = supertest; |