How Do I Use Parcel With Express JS Routers? #9411
Replies: 1 comment
-
Make sure that the Parcel watch command is running successfully and that it is watching the correct files. The watch script in your package.json seems to be using HTML files, but you are trying to route requests to a ticker router. Ensure that the watch command includes the necessary JavaScript files as well. "watch": "parcel watch src/home.html src/ticker.html src/ticker_router.js", app.use((req, res, next) => { app.use("/", routerHome); Router Path: tickerRouter.get("/:stock", (req, res) => { |
Beta Was this translation helpful? Give feedback.
-
I am trying to use a proxy server I created using Express JS, along with Parcel as my bundler. However, middleware for an endpoint does not seem to be used. The endpoint is supposed to run a get request but my app stays on the home page.
I am using the Parcel Watch command to bundle my entry files.
I am not sure what is going on. I can provide more info if needed. Any help is much appreciated!
server.js
`import express from "express";
import routerHome from "../js/home_router";
import tickerRouter from "../js/ticker_router";
const PORT = process.env.PORT || 3000;
const app = express();
app.use(express.static("dist"));
app.use(express.json());
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
next();
});
app.use("/", routerHome);
app.use("/ticker/:stock", tickerRouter);
app.listen(PORT, () => {
console.log("Proxy listening on port:", PORT);
});`
ticker_router.js file:
`import express from "express";
import path from "path";
const tickerRouter = express.Router();
tickerRouter.get("/", (req, res) => {
return res.sendFile(path.join("C:/Desktop/Ticqer2", "/dist/ticker.html"));
});
export default tickerRouter;
`
package.json:
`{
"name": "ticqer2",
"version": "1.0.0",
"description": "",
"type": "module",
"scripts": {
"test": "",
"watch": "parcel watch src/home.html src/ticker.html" ,
"build": "parcel build --dist-dir public"
},
"author": "",
"license": "ISC",
"dependencies": {
"cnbc-market": "^3.0.0",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"http-proxy-middleware": "^2.0.6",
"nodemon": "^3.0.1",
"yahoo-finance2": "^2.8.1"
},
"devDependencies": {
"@parcel/transformer-sass": "^2.10.2",
"assert": "^2.1.0",
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
"events": "^3.3.0",
"https-browserify": "^1.0.0",
"os-browserify": "^0.3.0",
"parcel": "^2.10.2",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"url": "^0.11.3",
"util": "^0.12.5"
}
}
`
Beta Was this translation helpful? Give feedback.
All reactions