diff --git a/Programming_Languages/NodeJS/Architecture/Ports.md b/Programming_Languages/NodeJS/Architecture/Ports.md index cfddf0f..f39c565 100644 --- a/Programming_Languages/NodeJS/Architecture/Ports.md +++ b/Programming_Languages/NodeJS/Architecture/Ports.md @@ -6,7 +6,7 @@ tags: - node-modules --- -Like Bash, Node utilises [environment variables](../Shell%20Scripting/Environmental%20and%20shell%20variables.md) and the syntax is the same since Node must run in a Bash environment or emulator (like GitBash on Windows). +# Ports When working in development we are able to specify the specific port from which we want top serve our application. In production, we do not always have this control: the port will most likely be set by the provider of the server environment. @@ -14,8 +14,6 @@ While we may not know the specific port, whichever it is, it will be accessible ````js const port = process.env.PORT || 3000; -app.listen(port, () => console.log(`Listening on ${port}`)); - ```` This way, if a port is set by the provider it will use it. If not, it will fall back to 3000. diff --git a/Programming_Languages/NodeJS/Modules/Built_In_Modules/events.md b/Programming_Languages/NodeJS/Modules/Native/events.md similarity index 100% rename from Programming_Languages/NodeJS/Modules/Built_In_Modules/events.md rename to Programming_Languages/NodeJS/Modules/Native/events.md diff --git a/Programming_Languages/NodeJS/Modules/Built_In_Modules/fs.md b/Programming_Languages/NodeJS/Modules/Native/fs.md similarity index 100% rename from Programming_Languages/NodeJS/Modules/Built_In_Modules/fs.md rename to Programming_Languages/NodeJS/Modules/Native/fs.md diff --git a/Programming_Languages/NodeJS/Modules/Built_In_Modules/http.md b/Programming_Languages/NodeJS/Modules/Native/http.md similarity index 100% rename from Programming_Languages/NodeJS/Modules/Built_In_Modules/http.md rename to Programming_Languages/NodeJS/Modules/Native/http.md diff --git a/Programming_Languages/NodeJS/Modules/Third_party/Morgan.md b/Programming_Languages/NodeJS/Modules/Third_party/Morgan.md new file mode 100644 index 0000000..43715b5 --- /dev/null +++ b/Programming_Languages/NodeJS/Modules/Third_party/Morgan.md @@ -0,0 +1,23 @@ +--- +tags: + - Programming_Languages + - backend + - node-js + - middleware +--- + +# Morgan + +Morgan is middleware that is used to log HTTP requests to the Express instance. + +```js +app.use(morgan('dev')) +``` +With Morgan in place, every time we run a request it will be logged on the console that is running our Node application, e.g: + +```plain +GET /api/courses 200 95 - 1.774 ms +``` +This uses the `tiny` default which logs the bare minimum giving us: request type; endpoint; response code; and time to execute. But there are more verbose settings. + +It defaults to logging on the console but can also be configured to write to a log file. diff --git a/Programming_Languages/NodeJS/Modules/Third_party/Nodemon.md b/Programming_Languages/NodeJS/Modules/Third_party/Nodemon.md new file mode 100644 index 0000000..e086fa4 --- /dev/null +++ b/Programming_Languages/NodeJS/Modules/Third_party/Nodemon.md @@ -0,0 +1,12 @@ +--- +tags: + - Programming_Languages + - backend + - node-js +--- + +# Nodemon + +We don't want to have to restart the local server every time we make a change to our files. We can use `nodemon` instead of `node` when running our `index.js` file so that file-changes are immediately registered without the need for a restart. + +This is a wrapper around the `node` command so it doesn't require any configuration. Once installed, update your start script from `node index.js` to `nodemon index.js`.