const fs = require('fs') const path = require('path') const bodyParser = require('body-parser'); const langDir = path.join(__dirname, '../src/lang') const oss = require('ali-oss'); const OSSCONFIG = { region: 'oss-cn-shenzhen', //云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,部署在服务端使用RAM子账号或STS,部署在客户端使用STS。 accessKeyId: 'LTAI4G6f1efh9idnE8DfA71X', accessKeySecret: 'UQBHqXh4saRBXZeOYAIhJhnuzIcyhR', bucket: '4dscene' } const store = oss(OSSCONFIG) var FtpDeploy = require("ftp-deploy"); var ftpDeploy = new FtpDeploy(); var config = { user: "xuzhihao", // Password optional, prompted if none given password: "xuzhihao123", host: "192.168.0.115", port: 2222, localRoot: './../common/data/', remoteRoot: "/测试服务器-120.25.146.52/root/user/java/apache-tomcat-8.0.47/webapps/4dkankan_v2/WEB-INF/classes/web/www/newsList", include: ["news.json", 'news-en.json'], exclude: ["dist/**/*.map", "node_modules/**", "node_modules/**/.*", ".git/**"], deleteRemote: false, forcePasv: true, sftp: true }; module.exports = function(app, server) { app.use('/dev', bodyParser.json()); app.use('/dev', bodyParser.urlencoded({ extended: false })); app.get('/dev/lang/:id', function(req, res) { let filePath = path.join(langDir, req.params.id + '/modules') const files = fs.readdirSync(filePath) let data = {} files.forEach(item => { delete require.cache[require.resolve(path.join(__dirname, `../src/lang/${req.params.id}/modules/${item}`))] data[item.replace(/\.\w+$/, '')] = require(path.join(__dirname, `../src/lang/${req.params.id}/modules/${item}`)) }) res.json({ ok: true, data }); }); app.post('/dev/lang/:id', async function(req, res) { await Object.keys(req.body).forEach(async item => { await fs.writeFile('./src/lang/' + req.params.id + `/modules/${item}.js`, 'module.exports = ' + JSON.stringify(req.body[item], null, 4), err => { res.json({ ok: true }); }) }) }); const localUrl = { zh: './../../common/data/news.json', en: './../../common/data/news-en.json' } app.get('/dev/news/:lang', async function (req, res) { res.json({data: require(localUrl[req.params.lang])}) }) app.post('/dev/news/update', async function(req, res) { const data = req.body.data const localUr1l = { zh: './../common/data/news.json', en: './../common/data/news-en.json' } await fs.writeFile(localUr1l[req.body.lang], `${JSON.stringify(data, null, 4)}`, async err => { console.log(err) ftpDeploy .deploy(config) .then(() => { res.json({ ok: true }); }) .catch(err => console.log(err)); // try { // await store.put(oss_path, OUTPUT) // console.log('success') // } catch (err) { // console.log(err) // } }) }) }