Created
March 4, 2020 15:45
-
-
Save lsmoura/354f676f42c1df7ec2a9ea9ee44de413 to your computer and use it in GitHub Desktop.
Init expressjs repo
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
#!/bin/sh | |
yarn init . -y | |
mkdir -p src/db/ | |
yarn add express winston express-winston | |
yarn add --dev eslint babel-eslint prettier nodemon | |
cat > src/logger.js <<EOF | |
import winston from 'winston'; | |
import expressWinston from 'express-winston'; | |
const logger = expressWinston.logger({ | |
transports: [ | |
new winston.transports.Console() | |
], | |
format: winston.format.combine( | |
winston.format.colorize(), | |
winston.format.json(), | |
), | |
meta: true, | |
msg: "HTTP {{req.method}} {{req.url}}", | |
expressFormat: true, | |
colorize: true, | |
ignoreRoute: function (req, res) { return false; }, | |
}); | |
export default logger; | |
EOF | |
cat > src/index.js <<EOF | |
import express from 'express'; | |
import logger from './logger'; | |
const PORT = process.env.PORT; | |
if (!PORT) { | |
throw new Error('PORT: no port value given'); | |
} | |
if (Number.isNaN(parseInt(PORT, 10)) { | |
throw new Error('PORT: invalid value given: ' PORT); | |
} | |
const app = express(); | |
app.use(logger); | |
app.listen(parseInt(PORT, 10), () => { | |
console.log('listening on port', portNumber); | |
}); | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment