-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
88 lines (80 loc) · 3 KB
/
Copy pathserver.js
File metadata and controls
88 lines (80 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require( "express" );
const next = require( "next" );
const dev = process.env.NODE_ENV !== "production";
const app = next( { dev } );
const handler = app.getRequestHandler();
const port = process.env.PORT || 3000;
app.prepare()
.then(() => {
const server = express();
const supportedCities = [
{
airportCode: "hsv",
city: "Huntsville",
id: 2,
sponsor: {
name: "CommentSold",
url: "https://commentsold.com"
},
venue: "Huntsville West"
},
{
airportCode: "bhm",
city: "Birmingham",
id: 54,
sponsor: {
name: "Innovation Depot",
url: "https://innovationdepot.org"
},
venue: "Innovation Depot"
}
];
// Show Huntsville's schedule on the home page
server.get("/", ( request, response ) => {
let city = {};
const match = supportedCities.filter( function( cityInfo ) {
if ( cityInfo.airportCode === "hsv" ) {
city = cityInfo;
return cityInfo;
}
} );
if ( match !== undefined && match.length !== 0 ) {
// We want to render the Huntsville schedule by default
app.render( request, response, "/city", { ...request.query, ...request.params, city } );
} else {
// user visited a city we don't yet support
response.statusCode = 404;
app.render( request, response, "/_error", { } );
}
} );
// Show us the schedule for the city given a specific airport code
server.get("/:airportCode", ( request, response ) => {
let city = {};
const match = supportedCities.filter( function( cityInfo ) {
if ( cityInfo.airportCode === request.params.airportCode ) {
city = cityInfo;
return cityInfo;
}
});
if ( match !== undefined && match.length !== 0 ) {
// user visited a city we support
app.render( request, response, "/city", { ...request.query, ...request.params, city } );
} else {
// user visited a city we don't yet support
response.statusCode = 404;
app.render( request, response, "/_error", { } );
}
} );
// Handle all other requests
server.get( "*", ( request, response ) => {
return handler( request, response );
} );
server.listen( port, error => {
if ( error ) throw error;
console.log( `> Ready on http://localhost:${port}` );
} );
} )
.catch( exception => {
console.error( exception.stack );
process.exit( 1 );
} );