Skip to content

Instantly share code, notes, and snippets.

@thiagocom
Last active November 22, 2019 18:52
Show Gist options
  • Save thiagocom/fdca71011bfb4ceda63153718fb71feb to your computer and use it in GitHub Desktop.
Save thiagocom/fdca71011bfb4ceda63153718fb71feb to your computer and use it in GitHub Desktop.
Basic server with express
const express = require("express")
const logger = require("morgan")
require("dotenv").config()
const routes = require("./routes")
const app = express()
app.set("port", process.env.PORT || 8080)
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
if (process.env.NODE_ENV == "production") {
const accessLogStream = rfs("app.log", {
size: "10M",
interval: "1d"
})
app.use(logger("combined", { stream: accessLogStream }))
} else {
app.use(logger("dev"))
}
app.use("/", routes)
app.use(async (err, req, res, next) => {
const statusCode = res.statusCode || 500
res.status(statusCode).json({ message: err.message })
})
module.exports = app
const app = require("./source/app")
const port = app.get("port")
app.listen(port, () => console.log(`Application running on port ${ port }`))
const router = require("express").Router()
const greetings = [
"Welcome to development world",
"This is Fun!",
"Development with node.js"
]
router.get("/", async (req, res) => {
const message = greetings[ Math.floor(Math.random() * greetings.length) ]
res.json({ message })
})
module.exports = router
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment