Last active
November 22, 2019 18:52
-
-
Save thiagocom/fdca71011bfb4ceda63153718fb71feb to your computer and use it in GitHub Desktop.
Basic server with express
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
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 |
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
const app = require("./source/app") | |
const port = app.get("port") | |
app.listen(port, () => console.log(`Application running on port ${ 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
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