Last Sync: 2022-07-12 19:30:03
This commit is contained in:
		
							parent
							
								
									5f4bb25404
								
							
						
					
					
						commit
						e5061e6af1
					
				
					 6 changed files with 36 additions and 3 deletions
				
			
		| 
						 | 
					@ -6,7 +6,7 @@ tags:
 | 
				
			||||||
  - node-modules
 | 
					  - 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. 
 | 
					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
 | 
					````js
 | 
				
			||||||
const port = process.env.PORT || 3000;
 | 
					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.
 | 
					This way, if a port is set by the provider it will use it. If not, it will fall back to 3000.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										23
									
								
								Programming_Languages/NodeJS/Modules/Third_party/Morgan.md
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								Programming_Languages/NodeJS/Modules/Third_party/Morgan.md
									
										
									
									
										vendored
									
									
										Normal file
									
								
							| 
						 | 
					@ -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. 
 | 
				
			||||||
							
								
								
									
										12
									
								
								Programming_Languages/NodeJS/Modules/Third_party/Nodemon.md
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Programming_Languages/NodeJS/Modules/Third_party/Nodemon.md
									
										
									
									
										vendored
									
									
										Normal file
									
								
							| 
						 | 
					@ -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`. 
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue