2017-01-12 04:14:49 +00:00
|
|
|
import fetch from 'node-fetch'
|
|
|
|
import qs from 'querystring'
|
2017-02-09 13:40:09 +00:00
|
|
|
import http from 'http'
|
2017-05-09 23:24:34 +00:00
|
|
|
import express from 'express'
|
2017-01-12 04:14:49 +00:00
|
|
|
|
|
|
|
import server from '../../dist/server/next'
|
|
|
|
import build from '../../dist/server/build'
|
2017-05-09 23:24:34 +00:00
|
|
|
import _export from '../../dist/server/export'
|
2017-01-12 04:14:49 +00:00
|
|
|
import _pkg from '../../package.json'
|
|
|
|
|
|
|
|
export const nextServer = server
|
|
|
|
export const nextBuild = build
|
2017-05-09 23:24:34 +00:00
|
|
|
export const nextExport = _export
|
2017-01-12 04:14:49 +00:00
|
|
|
export const pkg = _pkg
|
|
|
|
|
|
|
|
export function renderViaAPI (app, pathname, query = {}) {
|
2017-05-03 16:40:09 +00:00
|
|
|
const url = `${pathname}?${qs.stringify(query)}`
|
|
|
|
return app.renderToHTML({ url }, {}, pathname, query)
|
2017-01-12 04:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function renderViaHTTP (appPort, pathname, query = {}) {
|
|
|
|
const url = `http://localhost:${appPort}${pathname}?${qs.stringify(query)}`
|
|
|
|
return fetch(url).then((res) => res.text())
|
|
|
|
}
|
2017-02-09 13:40:09 +00:00
|
|
|
|
|
|
|
export async function startApp (app) {
|
|
|
|
await app.prepare()
|
|
|
|
const handler = app.getRequestHandler()
|
|
|
|
const server = http.createServer(handler)
|
|
|
|
server.__app = app
|
|
|
|
|
|
|
|
await promiseCall(server, 'listen')
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function stopApp (app) {
|
2017-05-09 23:24:34 +00:00
|
|
|
if (server.__app) {
|
|
|
|
await server.__app.close()
|
|
|
|
}
|
2017-02-09 13:40:09 +00:00
|
|
|
await promiseCall(server, 'close')
|
|
|
|
}
|
|
|
|
|
|
|
|
function promiseCall (obj, method, ...args) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const newArgs = [
|
|
|
|
...args,
|
|
|
|
function (err, res) {
|
|
|
|
if (err) return reject(err)
|
|
|
|
resolve(res)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
obj[method](...newArgs)
|
|
|
|
})
|
|
|
|
}
|
2017-02-26 19:45:16 +00:00
|
|
|
|
|
|
|
export function waitFor (millis) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, millis))
|
|
|
|
}
|
2017-05-09 23:24:34 +00:00
|
|
|
|
|
|
|
export async function startStaticServer (dir) {
|
|
|
|
const app = express()
|
|
|
|
const server = http.createServer(app)
|
|
|
|
app.use(express.static(dir))
|
|
|
|
|
|
|
|
await promiseCall(server, 'listen')
|
|
|
|
return server
|
|
|
|
}
|