Reinitialized commit. Update and actors overview with some filters.
This commit is contained in:
20
src/web/actors.js
Normal file
20
src/web/actors.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { fetchActors } from '../actors.js';
|
||||
|
||||
export async function fetchActorsApi(req, res) {
|
||||
const { actors, limit, total } = await fetchActors({
|
||||
query: req.query.q,
|
||||
requireAvatar: Object.hasOwn(req.query, 'avatar'),
|
||||
age: req.query.age?.split(',').map((age) => Number(age)),
|
||||
height: req.query.height?.split(',').map((height) => Number(height)),
|
||||
weight: req.query.weight?.split(',').map((weight) => Number(weight)),
|
||||
}, {
|
||||
page: Number(req.query.page) || 1,
|
||||
limit: Number(req.query.limit) || 50,
|
||||
});
|
||||
|
||||
res.send({
|
||||
actors,
|
||||
limit,
|
||||
total,
|
||||
});
|
||||
}
|
||||
8
src/web/root.js
Normal file
8
src/web/root.js
Normal file
@@ -0,0 +1,8 @@
|
||||
// https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-when-using-the-experimental-modules-flag/50052194#50052194
|
||||
import { dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const root = `${__dirname}/..`;
|
||||
|
||||
export default root;
|
||||
101
src/web/server.js
Normal file
101
src/web/server.js
Normal file
@@ -0,0 +1,101 @@
|
||||
// This file isn't processed by Vite, see https://github.com/vikejs/vike/issues/562
|
||||
// Consequently:
|
||||
// - When changing this file, you needed to manually restart your server for your changes to take effect.
|
||||
// - To use your environment variables defined in your .env files, you need to install dotenv, see https://vike.dev/env
|
||||
// - To use your path aliases defined in your vite.config.js, you need to tell Node.js about them, see https://vike.dev/path-aliases
|
||||
|
||||
// If you want Vite to process your server code then use one of these:
|
||||
// - vavite (https://github.com/cyco130/vavite)
|
||||
// - See vavite + Vike examples at https://github.com/cyco130/vavite/tree/main/examples
|
||||
// - vite-node (https://github.com/antfu/vite-node)
|
||||
// - HatTip (https://github.com/hattipjs/hattip)
|
||||
// - You can use Bati (https://batijs.github.io/) to scaffold a Vike + HatTip app. Note that Bati generates apps that use the V1 design (https://vike.dev/migration/v1-design) and Vike packages (https://vike.dev/vike-packages)
|
||||
|
||||
import config from 'config';
|
||||
import express from 'express';
|
||||
import Router from 'express-promise-router';
|
||||
import compression from 'compression';
|
||||
import { renderPage } from 'vike/server'; // eslint-disable-line import/extensions
|
||||
|
||||
// import root from './root.js';
|
||||
|
||||
import { fetchActorsApi } from './actors.js'; // eslint-disable-line import/extensions
|
||||
|
||||
const isProduction = process.env.NODE_ENV === 'production';
|
||||
|
||||
async function startServer() {
|
||||
const app = express();
|
||||
const router = Router();
|
||||
|
||||
app.use(compression());
|
||||
app.disable('x-powered-by');
|
||||
|
||||
router.use('/', express.static('public'));
|
||||
router.use('/', express.static('static'));
|
||||
router.use('/media', express.static(config.media.path));
|
||||
|
||||
// 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;
|
||||
router.use(sirv('dist/client'));
|
||||
} else {
|
||||
// We instantiate Vite's development server and integrate its middleware to our server.
|
||||
// ⚠️ We instantiate it only in development. (It isn't needed in production and it
|
||||
// would unnecessarily bloat our production server.)
|
||||
const vite = await import('vite');
|
||||
const viteDevMiddleware = (
|
||||
await vite.createServer({
|
||||
// root,
|
||||
server: { middlewareMode: true },
|
||||
})
|
||||
).middlewares;
|
||||
|
||||
router.use(viteDevMiddleware);
|
||||
}
|
||||
|
||||
router.get('/api/actors', fetchActorsApi);
|
||||
|
||||
// ...
|
||||
// Other middlewares (e.g. some RPC middleware such as Telefunc)
|
||||
// ...
|
||||
|
||||
// Vike middleware. It should always be our last middleware (because it's a
|
||||
// catch-all middleware superseding any middleware placed after it).
|
||||
router.get('*', async (req, res, next) => {
|
||||
const pageContextInit = {
|
||||
urlOriginal: req.originalUrl,
|
||||
};
|
||||
|
||||
const pageContext = await renderPage(pageContextInit);
|
||||
const { httpResponse } = pageContext;
|
||||
|
||||
if (!httpResponse) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
body, statusCode, headers, earlyHints,
|
||||
} = httpResponse;
|
||||
|
||||
if (res.writeEarlyHints) {
|
||||
res.writeEarlyHints({ link: earlyHints.map((e) => e.earlyHintLink) });
|
||||
}
|
||||
|
||||
headers.forEach(([name, value]) => res.setHeader(name, value));
|
||||
res.status(statusCode);
|
||||
// For HTTP streams use httpResponse.pipe() instead, see https://vike.dev/stream
|
||||
res.send(body);
|
||||
});
|
||||
|
||||
app.use(router);
|
||||
|
||||
const port = process.env.PORT || config.web.port || 3000;
|
||||
app.listen(port);
|
||||
|
||||
console.log(`Server running at http://localhost:${port}`);
|
||||
}
|
||||
|
||||
startServer();
|
||||
Reference in New Issue
Block a user