Skip to content

Commit

Permalink
Initial commit with simple express server setup
Browse files Browse the repository at this point in the history
Reinoptland committed Mar 18, 2020

Verified

This commit was signed with the committer’s verified signature. The key has expired.
lburgazzoli Luca Burgazzoli
0 parents commit cd2f790
Showing 4 changed files with 494 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
71 changes: 71 additions & 0 deletions index.js
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}`);
});
Loading

0 comments on commit cd2f790

Please sign in to comment.