Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mount HTML serving middlewares at config.base #91

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ async function injectViteIndexMiddleware(
) {
const config = await getViteConfig();

app.get("/*", async (req, res, next) => {
app.use(config.base, async (req, res, next) => {
if (req.method !== "GET") return next();

if (isIgnoredPath(req.path, req)) return next();

if (isStaticFilePath(req.path)) next();
Expand All @@ -230,11 +232,12 @@ async function injectViteIndexMiddleware(

async function injectIndexMiddleware(app: core.Express) {
const distPath = await getDistPath();
const config = await getViteConfig();

app.use("*", (req, res, next) => {
if (isIgnoredPath(req.baseUrl, req)) return next();
app.use(config.base, (req, res, next) => {
if (isIgnoredPath(req.path, req)) return next();

const indexPath = findClosestIndexToRoot(req.originalUrl, distPath);
const indexPath = findClosestIndexToRoot(req.path, distPath);
if (indexPath === undefined) return next();

const html = fs.readFileSync(indexPath, "utf8");
Expand Down
2 changes: 1 addition & 1 deletion tests/templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const testCase = async (template: string, done: () => void) => {

browser.then(async (browser) => {
const page = await browser.newPage();
await page.goto("http://localhost:3000");
await page.goto("http://localhost:3000/admin");
Copy link
Owner

@szymmis szymmis Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost got there 😅, what I suggest is to add base parameter to testCase function, that's set to / by default.

const testCase = async (template: string, done: () => void, base = "/") => {
Suggested change
await page.goto("http://localhost:3000/admin");
await page.goto(`http://localhost:3000${base}`);

That's because right now other test would fail (Well, maybe they wouldn't fail because if vite-express is mounted at root, it will serve the page at /admin, but it will definitely make things unclear). If you want to run them locally you can use npm run test or yarn test or any other package manager.

By specifying the parameter as default, you don't need to adjust other testCase invocations, only the one in Template "${template}" with custom inline config test here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made modifications to the code, but on windows, even after attempting to install with both npm and yarn, I consistently encounter the following error when running npm run test with or without administrative privileges:

TEST  Template "react"
FAIL  Error: EACCES: permission denied, scandir 'D:\vite-express\create-vite-express\templates\react\node_modules\.bin\browserslist'
FAIL  18 failed, 0 passed in 0.047706s

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's interesting, I'm running linux so that's why I didn't catch it, but thanks for bringing that up. You can push the code so I can take a look and run the CI if everything looks alright.


it("test set up");

Expand Down