Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #578 from nondanee/patch
Browse files Browse the repository at this point in the history
支持 CORS 预检
  • Loading branch information
Binaryify authored Aug 25, 2019
2 parents dbd1d7a + 52a4dd5 commit f848a00
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ exec('npm info NeteaseCloudMusicApi version', (err, stdout, stderr) => {

const app = express()

// CORS
// CORS & Preflight request
app.use((req, res, next) => {
if(req.path !== '/' && !req.path.includes('.')){
res.header({
res.set({
'Access-Control-Allow-Credentials': true,
'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',
'Content-Type': 'application/json; charset=utf-8'
})
}
next()
req.method === 'OPTIONS' ? res.status(204).end() : next()
})

// cookie parser
Expand Down Expand Up @@ -61,10 +61,10 @@ const special = {
}

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 question = require(path.join(__dirname, 'module', file))

app.use(route, (req, res) => {
let query = Object.assign({}, req.query, req.body, {cookie: req.cookies})
question(query, request)
Expand All @@ -75,7 +75,7 @@ fs.readdirSync(path.join(__dirname, 'module')).reverse().forEach(file => {
})
.catch(answer => {
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.status(answer.status).send(answer.body)
})
Expand Down

0 comments on commit f848a00

Please sign in to comment.