Initiate web server
This commit is contained in:
		
							parent
							
								
									7c74514f4f
								
							
						
					
					
						commit
						3cd55961cb
					
				
					 1 changed files with 39 additions and 20 deletions
				
			
		
							
								
								
									
										51
									
								
								index.js
									
										
									
									
									
								
							
							
						
						
									
										51
									
								
								index.js
									
										
									
									
									
								
							|  | @ -1,30 +1,49 @@ | ||||||
| // index.js
 | // index.js
 | ||||||
| // where your node app starts
 |  | ||||||
| 
 | 
 | ||||||
| // init project
 | // init project
 | ||||||
| require('dotenv').config(); | require('dotenv').config(); | ||||||
| var express = require('express'); | const express = require('express'); | ||||||
| var app = express(); | var app = express(); | ||||||
| 
 |  | ||||||
| // enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
 |  | ||||||
| // so that your API is remotely testable by FCC
 |  | ||||||
| var cors = require('cors'); | var cors = require('cors'); | ||||||
|  | 
 | ||||||
|  | const ParserAPI = require('./scripts/ParserAPI').ParserAPI; | ||||||
|  | 
 | ||||||
|  | class WebServer { | ||||||
|  | 	/* | ||||||
|  | 		Initiate the web server.  | ||||||
|  | 
 | ||||||
|  | 		@param {function} callback - The callback run before activating the server | ||||||
|  | 	*/ | ||||||
|  | 
 | ||||||
|  | 		constructor(callback) { | ||||||
| 			app.use(cors({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204
 | 			app.use(cors({ optionsSuccessStatus: 200 })); // some legacy browsers choke on 204
 | ||||||
| 			 | 			 | ||||||
|  | 
 | ||||||
|  | 			this.#setDefaults(); | ||||||
|  | 			(callback) ? this[`activity`] = callback() : null; | ||||||
|  | 
 | ||||||
|  | 			var listener = app.listen(process.env.PORT || 3000, () => { | ||||||
|  | 				console.log(`Active on port ${listener.address().port}.`); | ||||||
|  | 			}); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/* Configure the default responses for the web server. */ | ||||||
|  | 	#setDefaults() { | ||||||
| 		// http://expressjs.com/en/starter/static-files.html
 | 		// http://expressjs.com/en/starter/static-files.html
 | ||||||
| app.use(express.static('public')); | 		app.use(express.static(__dirname + WebServer.paths['assets'])); | ||||||
| 
 | 
 | ||||||
| 		// http://expressjs.com/en/starter/basic-routing.html
 | 		// http://expressjs.com/en/starter/basic-routing.html
 | ||||||
| app.get('/', function (req, res) { | 		app.get("/", function (REQUEST, RESPONSE) { | ||||||
|   res.sendFile(__dirname + '/views/index.html'); | 			RESPONSE.sendFile(__dirname + WebServer[`paths`][`default`]); | ||||||
| 		}); | 		}); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	static paths = { | ||||||
|  |     "assets": "/public", | ||||||
|  |     "default": '/views/index.html' | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | new WebServer(() => {return (new ParserAPI(app))}); | ||||||
| 
 | 
 | ||||||
| // your first API endpoint...
 |  | ||||||
| app.get('/api/hello', function (req, res) { |  | ||||||
|   res.json({ greeting: 'hello API' }); |  | ||||||
| }); |  | ||||||
| 
 | 
 | ||||||
| // listen for requests :)
 |  | ||||||
| var listener = app.listen(process.env.PORT || 3000, function () { |  | ||||||
|   console.log('Your app is listening on port ' + listener.address().port); |  | ||||||
| }); |  | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue