提交 52a4dd54 编写于 作者: N Nzix

fit for preflight request

上级 dbd1d7ab
...@@ -19,18 +19,18 @@ exec('npm info NeteaseCloudMusicApi version', (err, stdout, stderr) => { ...@@ -19,18 +19,18 @@ exec('npm info NeteaseCloudMusicApi version', (err, stdout, stderr) => {
const app = express() const app = express()
// CORS // CORS & Preflight request
app.use((req, res, next) => { app.use((req, res, next) => {
if(req.path !== '/' && !req.path.includes('.')){ if(req.path !== '/' && !req.path.includes('.')){
res.header({ res.set({
'Access-Control-Allow-Credentials': true, 'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Origin': req.headers.origin || '*', 'Access-Control-Allow-Origin': req.headers.origin || '*',
'Access-Control-Allow-Headers': 'X-Requested-With', 'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS', 'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
'Content-Type': 'application/json; charset=utf-8' 'Content-Type': 'application/json; charset=utf-8'
}) })
} }
next() req.method === 'OPTIONS' ? res.status(204).end() : next()
}) })
// cookie parser // cookie parser
...@@ -61,7 +61,7 @@ const special = { ...@@ -61,7 +61,7 @@ const special = {
} }
fs.readdirSync(path.join(__dirname, 'module')).reverse().forEach(file => { fs.readdirSync(path.join(__dirname, 'module')).reverse().forEach(file => {
if(!(/\.js$/i.test(file))) return if(!file.endsWith('.js')) return
let route = (file in special) ? special[file] : '/' + file.replace(/\.js$/i, '').replace(/_/g, '/') let route = (file in special) ? special[file] : '/' + file.replace(/\.js$/i, '').replace(/_/g, '/')
let question = require(path.join(__dirname, 'module', file)) let question = require(path.join(__dirname, 'module', file))
...@@ -75,7 +75,7 @@ fs.readdirSync(path.join(__dirname, 'module')).reverse().forEach(file => { ...@@ -75,7 +75,7 @@ fs.readdirSync(path.join(__dirname, 'module')).reverse().forEach(file => {
}) })
.catch(answer => { .catch(answer => {
console.log('[ERR]', decodeURIComponent(req.originalUrl)) console.log('[ERR]', decodeURIComponent(req.originalUrl))
if(answer.body.code =='301') answer.body.msg = '需要登录' if(answer.body.code == '301') answer.body.msg = '需要登录'
res.append('Set-Cookie', answer.cookie) res.append('Set-Cookie', answer.cookie)
res.status(answer.status).send(answer.body) res.status(answer.status).send(answer.body)
}) })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册