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

Commit

Permalink
Fix build bug
Browse files Browse the repository at this point in the history
  • Loading branch information
trazyn committed Jan 22, 2019
1 parent f1b73fc commit 5a8318b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
8 changes: 8 additions & 0 deletions config/webpack.config.production.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export default {
from: `${config.assets}/**/*`,
to: `${config.dist}`,
},
{
from: `${path.resolve(__dirname, '../NeteaseCloudMusicApi')}/module/*`,
to: config.dist,
},
{
from: `${path.resolve(__dirname, '../NeteaseCloudMusicApi')}/util/*`,
to: config.dist,
},
{
from: path.resolve(__dirname, '../package.json'),
to: config.dist,
Expand Down
51 changes: 28 additions & 23 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,46 @@ app.use((req, res, next) => {
next();
});

function isDev() {
return process.mainModule.filename.indexOf('app.asar') === -1;
}

function mount(proxy) {
let apisDir = '../NeteaseCloudMusicApi/module';
let special = {
'daily_signin.js': '/daily_signin',
'fm_trash.js': '/fm_trash',
'personal_fm.js': '/personal_fm'
};

fs.readdirSync(path.join(__dirname, apisDir)).reverse().forEach(file => {
fs.readdirSync(path.join(__dirname, `${isDev() ? '..' : '.'}/NeteaseCloudMusicApi/module`)).reverse().forEach(file => {
if (!/\.js$/i.test(file)) {
return;
}
let route = (file in special) ? special[file] : '/' + file.replace(/\.js$/i, '').replace(/_/g, '/');
let question = require(path.join(__dirname, apisDir, file));
let request = require(`../NeteaseCloudMusicApi/util/request`);

console.log('Mount: ', route);
try {
// https://stackoverflow.com/questions/42797313/webpack-dynamic-module-loader-by-require
let route = (file in special) ? special[file] : '/' + file.replace(/\.js$/i, '').replace(/_/g, '/');
let question = require('../NeteaseCloudMusicApi/module/' + file);
let request = require('../NeteaseCloudMusicApi/util/request');

app.use(route, (req, res) => {
let query = Object.assign({}, req.query, req.body, { cookie: req.cookies }, { proxy });
question(query, request)
.then(answer => {
console.log('[OK]', decodeURIComponent(req.originalUrl));
res.append('Set-Cookie', answer.cookie);
res.status(answer.status).send(answer.body);
})
.catch(answer => {
console.log('[ERR]', decodeURIComponent(req.originalUrl));
if (answer.body.code === 301) {
answer.status = 200;
}
res.append('Set-Cookie', answer.cookie);
res.status(answer.status).send(answer.body);
});
});
app.use(route, (req, res) => {
let query = Object.assign({}, req.query, req.body, { cookie: req.cookies }, { proxy });
question(query, request)
.then(answer => {
res.append('Set-Cookie', answer.cookie);
res.status(answer.status).send(answer.body);
})
.catch(answer => {
if (answer.body.code === 301) {
answer.status = 200;
}
res.append('Set-Cookie', answer.cookie);
res.status(answer.status).send(answer.body);
});
});
} catch (ex) {
console.error(ex);
}
});

app.use('/api/home', require('./router/home'));
Expand Down

0 comments on commit 5a8318b

Please sign in to comment.