| 
									
										
										
										
											2019-06-01 00:56:07 +09:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2019-06-01 00:56:07 +09:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const http = require('http'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | let server; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const localFolder = __dirname; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 12:12:45 +02:00
										 |  |  | function writeNotFound(res) { | 
					
						
							|  |  |  |   res.writeHead(404, {'Content-Type': 'text/html'}); | 
					
						
							|  |  |  |   res.end('<h1>404, Not Found!</h1>'); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-01 00:56:07 +09:00
										 |  |  | function requestHandler(req, res) { | 
					
						
							|  |  |  |   if (req.url === '/close') { | 
					
						
							|  |  |  |     res.end('server closing'); | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     setTimeout(() => { | 
					
						
							|  |  |  |       process.exit(0); | 
					
						
							|  |  |  |     }, 1000); | 
					
						
							| 
									
										
										
										
											2019-06-01 00:56:07 +09:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2019-08-29 12:12:45 +02:00
										 |  |  |     const file = path.resolve(localFolder, req.url); | 
					
						
							|  |  |  |     if (!file.startsWith(localFolder + '/')) { | 
					
						
							|  |  |  |       writeNotFound(res); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-01 00:56:07 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     fs.readFile(file, function(err, contents) { | 
					
						
							|  |  |  |       if (!err) { | 
					
						
							|  |  |  |         res.end(contents); | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2019-08-29 12:12:45 +02:00
										 |  |  |         writeNotFound(res); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-06-01 00:56:07 +09:00
										 |  |  |       }; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-29 12:12:45 +02:00
										 |  |  | server = http.createServer(requestHandler).listen(8080); |