-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
45 lines (36 loc) · 1.21 KB
/
index.js
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
import { Nuxt, Builder } from 'nuxt'
import express from 'express'
import loopback from 'loopback'
import boot from 'loopback-boot'
import explorer from 'loopback-component-explorer'
import cookieParser from 'cookie-parser'
import config from './nuxt.config'
import { apiURL, host, port } from './config.json'
/* Set API */
const app = loopback()
/* Build Nuxt */
config.dev = !(process.env.NODE_ENV === 'production')
const nuxt = new Nuxt(config)
/* Build Loopback */
boot(app, 'server')
app.use(cookieParser('super-secret-key'))
app.use(loopback.token({
cookies: ['AuthToken'],
model: app.models.AccessToken
}))
app.enableAuth()
/* Build DEV environment */
if (config.dev) {
const builder = new Builder(nuxt)
builder.build()
const mountPath = `${apiURL}/explorer`
explorer(app, { basePath: apiURL, mountPath })
app.get(apiURL, app.loopback.status())
console.log('Explore your REST API at %s%s\n', `${host}:${port}`, mountPath)
}
/* Use loopback for all requests starting with `/api` ... */
app.all(new RegExp(`^${apiURL}.*`), app.loopback.Router())
/* ... and Nuxt renderer for other requests */
app.all(new RegExp(`^(?!${apiURL}).*`), nuxt.render)
/* Listen to the app */
app.listen(port, host)