-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit with simple express server setup
0 parents
commit cd2f790
Showing
4 changed files
with
494 additions
and
0 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 @@ | ||
/node_modules |
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,71 @@ | ||
const express = require("express"); | ||
const app = express(); | ||
|
||
/** | ||
* Config vars | ||
*/ | ||
|
||
const PORT = process.env.PORT || 4000; | ||
|
||
/** | ||
* Middlewares | ||
*/ | ||
|
||
/** | ||
* morgan: | ||
* | ||
* simple logging middleware so you can see | ||
* what happened to your request | ||
* | ||
* example: | ||
* | ||
* METHOD PATH STATUS RESPONSE_TIME - Content-Length | ||
* | ||
* GET / 200 1.807 ms - 15 | ||
* POST /echo 200 10.251 ms - 26 | ||
* POST /puppies 404 1.027 ms - 147 | ||
* | ||
* github: https://github.com/expressjs/morgan | ||
* | ||
*/ | ||
|
||
const loggerMiddleWare = require("morgan"); | ||
app.use(loggerMiddleWare("dev")); | ||
|
||
/** | ||
* | ||
* express.json(): | ||
* be able to read request bodies of JSON requests | ||
* a.k.a. body-parser | ||
* Needed to be able to POST / PUT / PATCH | ||
* | ||
* docs: https://expressjs.com/en/api.html#express.json | ||
* | ||
*/ | ||
|
||
const bodyParserMiddleWare = express.json(); | ||
app.use(bodyParserMiddleWare); | ||
|
||
/** | ||
* Routes | ||
* | ||
*/ | ||
|
||
// GET endpoint for testing purposes, can be removed | ||
app.get("/", (req, res) => { | ||
res.send("Hi from express"); | ||
}); | ||
|
||
// POST endpoint for testing purposes, can be removed | ||
app.post("/echo", (req, res) => { | ||
res.json({ | ||
youPosted: { | ||
...req.body | ||
} | ||
}); | ||
}); | ||
|
||
// Listen for connections on specified port (default is port 4000) | ||
app.listen(PORT, () => { | ||
console.log(`Listening on port: ${PORT}`); | ||
}); |
Oops, something went wrong.