如何解决Heroku TypeError:路径必须是绝对路径或将根目录指定为res.sendFile
我有以下问题。更新仪表板页面时,我在Heroku上始终收到此错误消息。我在App.tsx中使用react-router,并且Dashboard Route是“受保护的路由”。我也有一个登录路线。如果您还没有jwt令牌,将首先调用它。此后,如果登录成功并且您已经具有jwt令牌,那么您将被重定向到仪表板,然后您将被直接重定向到仪表板。下面是代码。
我的问题: 刷新仪表板时,出现以下错误:Internal Server Error 对于Heroku,它看起来像这样:
2020-09-21T15:23:42.881152+00:00 app[web.1]: [[32minfo[39m]: 2020-09-21T15:23:42.880Z: /dashboard
2020-09-21T15:23:42.886899+00:00 app[web.1]: TypeError: path must be absolute or specify root to res.sendFile
2020-09-21T15:23:42.886900+00:00 app[web.1]: at ServerResponse.sendFile (/app/node_modules/express/lib/response.js:425:11)
2020-09-21T15:23:42.886901+00:00 app[web.1]: at /app/build/index.js:50:9
2020-09-21T15:23:42.886901+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2020-09-21T15:23:42.886902+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/route.js:137:13)
2020-09-21T15:23:42.886902+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2020-09-21T15:23:42.886902+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2020-09-21T15:23:42.886903+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:281:22
2020-09-21T15:23:42.886903+00:00 app[web.1]: at param (/app/node_modules/express/lib/router/index.js:354:14)
2020-09-21T15:23:42.886904+00:00 app[web.1]: at param (/app/node_modules/express/lib/router/index.js:365:14)
2020-09-21T15:23:42.886904+00:00 app[web.1]: at Function.process_params (/app/node_modules/express/lib/router/index.js:410:3)
2020-09-21T15:23:42.886904+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:275:10)
2020-09-21T15:23:42.886904+00:00 app[web.1]: at SendStream.error (/app/node_modules/serve-static/index.js:121:7)
2020-09-21T15:23:42.886905+00:00 app[web.1]: at SendStream.emit (events.js:315:20)
2020-09-21T15:23:42.886905+00:00 app[web.1]: at SendStream.error (/app/node_modules/send/index.js:270:17)
2020-09-21T15:23:42.886905+00:00 app[web.1]: at SendStream.onStatError (/app/node_modules/send/index.js:421:12)
2020-09-21T15:23:42.886906+00:00 app[web.1]: at next (/app/node_modules/send/index.js:735:16)
我的代码:
App.tsx
return (
<div className="App">
<Router>
<Switch>
<Route exact path="/" component={Login} />
<ProtectedRoute path="/dashboard" component={Dashboard} />
<Route component={NoMatch} />
</Switch>
</Router>
</div>
);
}
LoginPage.tsx:
const loginClick = () => {
axios.post('/api/users/auth',{
email: email,password: password
}).then(res => {
localStorage.setItem("jwt-token",res.data.token);
history.push("/dashboard")
})
}
<React.Fragment>
{isAuthenticated() ? <Redirect to="/dashboard" /> :
<div>
<TextField
variant="outlined"
required
label="Email Address"
name="email"
autoComplete="email"
autoFocus
onChange={handleEmailChange}
/>
</div>
<div>
<TextField
variant="outlined"
required
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
onChange={handlePasswordChange}
/>
</div>
<div style={{ marginTop: 30 }}>
<Button
onClick={() => loginClick()}
variant="contained"
style={{ color: "white",backgroundColor: COLORS.primary }}
>
Anmelden
</Button>
</div>
}
</React.Fragment >
index.ts(后端):
import mongoose from "mongoose";
import express,{ Application,Request,Response,NextFunction } from "express";
import path from "path";
import tableData from "./api/tableData";
import tasks from "./api/tasks";
import companyName from "./api/companyNames";
import legalForm from "./api/legalForm";
import user from "./api/user"
import compression from "compression";
import logger from "./utils/logger";
import dotenv from "dotenv";
import bodyParser from "body-parser";
import http from "http";
import https from "https"
const loggingMiddleware = (req: Request,res: Response,next: NextFunction) => {
logger.info(`${new Date().toISOString()}: ${req.originalUrl}`);
next();
};
dotenv.config();
const app: Application = express();
const port = process.env.PORT || process.env.PORT_DEV;
const url =
process.env.MONGO_DB_MASTER ||
process.env.MONGO_DB_STAGING ||
process.env.MONGO_DB_LOCAL;
http.globalAgent.maxSockets = Infinity;
https.globalAgent.maxSockets = Infinity;
app.use(loggingMiddleware);
//app.use(express.static(__dirname + '/public',{ maxAge: 31557600 }));
app.set("query parser","simple")
app.use(compression());
app.use(bodyParser.json());
mongoose
.connect(`${url}`,{
useNewUrlParser: true,useUnifiedTopology: true,useFindAndModify: true,useCreateIndex: true,})
.then(() => logger.info("Database Connected Successfully"))
.catch((err) => logger.error(`Database Connection Error ${err}`));
app.use("/api",[tableData,tasks,companyName,legalForm,user]);
app.use(express.static(path.join("client/build")));
app.get("*",(req: Request,res: Response) => {
//res.setHeader('Cache-Control','public,max-age=86400')
res.sendFile(path.join("client/build/index.html"));
logger.info(__dirname);
});
app.listen(port,() => {
logger.info(`Server is running on port ${port}`);
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。