55 lines
2.4 KiB
JavaScript
55 lines
2.4 KiB
JavaScript
import config from 'config';
|
|
import express from 'express';
|
|
import bodyParser from 'express';
|
|
import compression from 'compression';
|
|
import { renderPage, createDevMiddleware } from 'vike/server';
|
|
import multer from 'multer';
|
|
import { root } from '#web/root.js';
|
|
import { uploadFilesApi } from '#web/files.js';
|
|
const isProduction = process.env.NODE_ENV === 'production';
|
|
export async function initServer() {
|
|
const app = express();
|
|
// const upload = multer({ dest: './uploads' });
|
|
const upload = multer({ dest: './uploads/temp' });
|
|
app.use(compression());
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded());
|
|
// Vite integration
|
|
if (isProduction) {
|
|
// In production, we need to serve our static assets ourselves.
|
|
// (In dev, Vite's middleware serves our static assets.)
|
|
const sirv = (await import('sirv')).default;
|
|
app.use(sirv(`${root}/dist/client`));
|
|
}
|
|
else {
|
|
const { devMiddleware } = await createDevMiddleware({ root });
|
|
app.use(devMiddleware);
|
|
}
|
|
// app.post('/api/files', uploadFilesApi);
|
|
app.post('/api/files', upload.array('files'), uploadFilesApi);
|
|
// Vike middleware. It should always be our last middleware (because it's a
|
|
// catch-all middleware superseding any middleware placed after it).
|
|
app.get('*', async (req, res) => {
|
|
const pageContextInit = {
|
|
urlOriginal: req.originalUrl,
|
|
headersOriginal: req.headers
|
|
};
|
|
const pageContext = await renderPage(pageContextInit);
|
|
if (pageContext.errorWhileRendering) {
|
|
// Install error tracking here, see https://vike.dev/error-tracking
|
|
}
|
|
const { httpResponse } = pageContext;
|
|
if (res.writeEarlyHints)
|
|
res.writeEarlyHints({ link: httpResponse.earlyHints.map((e) => e.earlyHintLink) });
|
|
httpResponse.headers.forEach(([name, value]) => res.setHeader(name, value));
|
|
res.status(httpResponse.statusCode);
|
|
// For HTTP streams use pageContext.httpResponse.pipe() instead, see https://vike.dev/streaming
|
|
res.send(httpResponse.body);
|
|
});
|
|
const host = process.env.HOST || config.web.host || 'localhost';
|
|
const port = process.env.PORT || config.web.port || 3000;
|
|
app.listen(port, host, () => {
|
|
console.log(`Server running at http://${host}:${port}`);
|
|
});
|
|
}
|
|
//# sourceMappingURL=server.js.map
|