-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateApp.js
More file actions
41 lines (31 loc) · 1.03 KB
/
createApp.js
File metadata and controls
41 lines (31 loc) · 1.03 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
import { App } from '@tinyhttp/app'
import { logger } from '@tinyhttp/logger'
import serve from 'serve-static'
import Bundler from 'parcel'
import { getCountries } from './functions/countries.js'
import { renderPage } from './src/util/server/renderPage.js'
export const createApp = async () => {
const app = new App()
const isProd = process.env.NODE_ENV === 'production'
// API functions
app.get('/api/countries', getCountries)
if (isProd) {
const routes = ['/', '/countries', '/about']
// Prerender each page in production
routes.forEach((route) => app.get(route, renderPage(route)))
return app.use(serve('dist'))
} else {
const bundler = new Bundler('./index.html')
app
.use(logger())
.use(serve('dist'))
.get('*', (_, res) => {
// Use a single index.html file in production
res.sendFile(`${process.cwd()}/dist/index.html`)
})
console.log(`Bundling frontend with Parcel...`)
// Bundle pages, then start an app
await bundler.bundle()
return app
}
}