我已经部署了后端和前端,但是前端无法连接到我的nodejs express后端,这给了我cors错误。在本地运行时,一切正常。 这是我后端的代码。
const express = require('express');
const cors = require('cors');
const StudentVue = require('studentvue.js')
const app = express();
app.use(cors())
const url = 'https://md-mcps-psv.edupoint.com/';
app.options('*', function (req, res) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Methods', '*');
res.setHeader("Access-Control-Allow-Headers", "*");
res.end();
});
app.options('*', cors())
app.use(express.json());
app.post('/done', async (req, res)=>{
StudentVue.login(url, req.body.name.toString(), req.body.password.toString()).then(client =>{
client.getGradebook().then(grades => {
let parsed = JSON.parse(grades)
if(parsed.RT_ERROR){
res.json({
message:"Invalid username or password"
})
}else{
res.json(parsed.Gradebook.Courses.Course)
}
})
})
})
错误是
VM378:1 POST https://mcpshelper-server.herokuapp.com/ 404 (Not Found)
(anonymous) @ VM378:1
(anonymous) @ client.js:15
mcpsvuehelper.surge.sh/:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0