feat: Create web server
This commit is contained in:
		
							parent
							
								
									b20ba578cb
								
							
						
					
					
						commit
						2ec229b1f6
					
				
					 1 changed files with 43 additions and 21 deletions
				
			
		
							
								
								
									
										64
									
								
								index.js
									
										
									
									
									
								
							
							
						
						
									
										64
									
								
								index.js
									
										
									
									
									
								
							|  | @ -5,28 +5,50 @@ | |||
| var express = require('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'); | ||||
| app.use(cors({optionsSuccessStatus: 200}));  // some legacy browsers choke on 204
 | ||||
| const Messaging = require('./scripts/messaging').Messaging; | ||||
| var TimestampAPI = require('./scripts/timestampAPI').TimestampAPI; | ||||
| 
 | ||||
| // http://expressjs.com/en/starter/static-files.html
 | ||||
| app.use(express.static('public')); | ||||
| class WebServer { | ||||
|   /* | ||||
|     Initiate the web server.  | ||||
| 
 | ||||
| // http://expressjs.com/en/starter/basic-routing.html
 | ||||
| app.get("/", function (req, res) { | ||||
|   res.sendFile(__dirname + '/views/index.html'); | ||||
| }); | ||||
|     @param {function} callback - The callback run before initiating the server | ||||
|   */ | ||||
|   constructor(callback) { | ||||
|     // enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
 | ||||
|     // so that your API is remotely testable by FCC 
 | ||||
|     var cors = require('cors'); | ||||
|     app.use(cors({optionsSuccessStatus: 200}));  // some legacy browsers choke on 204
 | ||||
| 
 | ||||
|     this.#setDefaults(); | ||||
|      | ||||
|     if (callback) { | ||||
|       this[`activity`] = callback(); | ||||
|     } | ||||
|      | ||||
|     // Listen on port set in environment variable or default to 3000
 | ||||
|     var listener = app.listen(process.env.PORT || 3000, function () { | ||||
|       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
 | ||||
|     app.use(WebServer.paths['assets'], express.static(__dirname + WebServer.paths['assets'])); | ||||
| 
 | ||||
|     // http://expressjs.com/en/starter/basic-routing.html
 | ||||
|     app.get("/", function (REQUEST, RESPONSE) { | ||||
|       RESPONSE.sendFile(__dirname + WebServer[`paths`][`default`]); | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|   static paths = { | ||||
|     "assets": "/public", | ||||
|     "default": '/views/index.html' | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| new WebServer(() => {return (new TimestampAPI(app))}); | ||||
| 
 | ||||
| 
 | ||||
| // your first API endpoint... 
 | ||||
| app.get("/api/hello", function (req, res) { | ||||
|   res.json({greeting: 'hello API'}); | ||||
| }); | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| // Listen on port set in environment variable or default to 3000
 | ||||
| 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