Files
spacedrive/apps/landing/server/index.ts
Oscar Beaumont ce6d2d2fda Eng 494 adding a location does not update the UI (#728)
* `sd_init.json` support

* bruh

* Add `sd_init.json` to developer docs

* Ran cargo clippy --fix to fix warnings

* Dafuq, cargo clippy --fix messed up cargo fmt

---------

Co-authored-by: Ericson Soares <ericson.ds999@gmail.com>
2023-04-20 22:05:57 +00:00

46 lines
1.2 KiB
TypeScript

import compression from 'compression';
import express from 'express';
import { renderPage } from 'vite-plugin-ssr';
const isProduction = process.env.NODE_ENV === 'production';
const root = `${__dirname}/..`;
startServer();
async function startServer() {
const app = express();
app.use(compression());
if (isProduction) {
const sirv = require('sirv');
app.use(sirv(`${root}/dist/client`));
} else {
const vite = require('vite');
const viteDevMiddleware = (
await vite.createServer({
root,
server: { middlewareMode: true }
})
).middlewares;
app.use(viteDevMiddleware);
}
app.get('*', async (req, res, next) => {
const url = req.originalUrl;
const pageContextInit = {
urlOriginal: url
};
const pageContext = await renderPage(pageContextInit);
const { httpResponse } = pageContext;
if (!httpResponse) return next();
const { body, statusCode, contentType } = httpResponse;
res.status(statusCode).type(contentType).send(body);
});
const port = process.env.PORT || 3000;
// @ts-ignore: I don't get why this isn't valid they have a definition matching this.
app.listen(port, '0.0.0.0');
console.log(`Server running at http://localhost:${port}`);
}