最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

http status code 403 - express app gives 403 forbidden error when react app is served - Stack Overflow

programmeradmin1浏览0评论

I have made a frontend with react-app-rewired and react-router. In the development server (react-app-rewired start command) it works perfectly when I put 'http://localhost:3000/something' in my address bar but when I build it and serve it with my express backend (which also talks to a database) and put 'http://localhost:8080/something' it gives a 403 error. Just putting 'http://localhost:8080' works perfectly and also navigating through buttons to 'http://localhost:8080/something' works

ForbiddenError: Forbidden
    at createHttpError (/node_modules/send/index.js:979:12)
    at SendStream.error (/node_modules/send/index.js:270:31)
    at SendStream.pipe (/node_modules/send/index.js:549:12)
    at sendfile (/node_modules/express/lib/response.js:1139:8)
    at ServerResponse.sendFile (/node_modules/express/lib/response.js:450:3)
    at /server/index.js:25:9
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
    at next (/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/node_modules/express/lib/router/route.js:119:3)
    at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)

this is how I deploy my frontend (snippet from /server/index.js):

try {
  app.use(express.static(__dirname + "/../client/build/"));
  app.listen(PORT, () => {
    console.log(`Server listening on ${PORT}`);
  });
  app.get("*", (req, res) => {
    res.sendFile(__dirname + "/../client/build/index.html");
  });
}
catch (error) { console.log(error) }

after this function all my request handlers for talking back and forth between my frontend and the database, all in this way

app.post("/loginTeacher", (req, res) => {
  (async () => {
    try {
      if (checkRequest(req)) {
        // Try login
        user = await login(req["body"]["name"], req["body"]["surname"], req["body"]["sha256"], req["body"]["md5"])
        sessionid = user[0]
        userid = user[1]
        privilege = user[2]
        if (sessionid == "Invalid credentials") { res.status(400).send(sessionid) } //Invalid credentials
        else { res.status(200).send({ "sessionid": sessionid, "userid": userid, "privilege": privilege }) } //Successfully logged in
      }
      else { res.status(400).send("Invalid credentials") } // Invalid credentials
    }
    catch (err) {
      res.status(500).send("Server error: " + err) // Server error
    }
  })();
})

I did some research but didn't find anything relating this. (maybe because I don't use the right words but English isn't my first language)

发布评论

评论列表(0)

  1. 暂无评论