Skip to content

Instantly share code, notes, and snippets.

@lsmoura
Created March 4, 2020 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmoura/354f676f42c1df7ec2a9ea9ee44de413 to your computer and use it in GitHub Desktop.
Save lsmoura/354f676f42c1df7ec2a9ea9ee44de413 to your computer and use it in GitHub Desktop.
Init expressjs repo
#!/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