Skip to content

Commit

Permalink
Merge pull request #851 from hackclub/vercel-fix
Browse files Browse the repository at this point in the history
move file routing logic to index.js so vercel routing works
  • Loading branch information
recursiveforte authored Aug 22, 2024
2 parents 1c8b6a4 + 8146baf commit 5839081
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
28 changes: 28 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import deleteFile from "../backend/api/deleteFile.js";

import { supabase } from "../backend/api/supabase.js";

import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import path from 'path';

const app = express();

app.use(express.json());
Expand Down Expand Up @@ -55,4 +59,28 @@ app.get('/welcome-qr-code', (req, res) => {
res.redirect('/assembly');
});

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

app.use(express.static(join(__dirname, '../dist')));

app.use((req, res, next) => {
let pathName = req.path;

if (pathName === "/") pathName = "/index";

if (req.path.indexOf('.') === -1) {
const file = path.join(__dirname, '../dist', `${pathName}.html`);
res.sendFile(file, (err) => {
if (err) next();
});
} else {
next();
}
});

app.use((req, res, next) => {
const file = path.join(__dirname, '../dist', `404.html`);
res.status(404).sendFile(file);
});
export default app;
31 changes: 0 additions & 31 deletions dev_server.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
import app from './api/index.js'
import './build.js'

import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

import express from 'express';
import path from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

app.use(express.static(join(__dirname, 'dist')));

app.use((req, res, next) => {
let pathName = req.path;

if (pathName === "/") pathName = "/index";

if (req.path.indexOf('.') === -1) {
const file = path.join(__dirname, 'dist', `${pathName}.html`);
res.sendFile(file, (err) => {
if (err) next();
});
} else {
next();
}
});

app.use((req, res, next) => {
const file = path.join(__dirname, 'dist', `404.html`);
res.status(404).sendFile(file);
});

const port = process.env.PORT || 3000;

app.listen(port, () => {
Expand Down

0 comments on commit 5839081

Please sign in to comment.