当前位置:   article > 正文

使用axios+node.js实现读取修改本地json文件,前后端交互_axios修改本地json

axios修改本地json

直接使用以下代码

1、前端代码

1.1、html文件,axios直接使用cdn

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>test</title>
  6. <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
  7. <script src="https://unpkg.com/vue@2.6.14/dist/vue.min.js"></script>
  8. <!-- 引入样式 -->
  9. <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  10. <!-- 引入组件库 -->
  11. <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  12. </head>
  13. <body>
  14. <div id="app">
  15. <p>{{ name }}</p>
  16. <span>{{ mana }}</span><span>+{{ speed }}</span>
  17. <p>{{ }}</p>
  18. <button @click="getdata">加载存档</button>
  19. <button @click="savedata">存档</button>
  20. </div>
  21. </body>
  22. </html>

1.2、javascript代码

  1. <script>
  2. //import axios from 'axios'
  3. //const axios = require('axios');
  4. //Vue.prototype.$http=axios
  5. new Vue({
  6. el: '#app',
  7. data: {
  8. name:'无名氏',
  9. message: '斗气大陆',
  10. mana:0,
  11. speed:1
  12. },
  13. methods:{
  14. gradeup:function(){
  15. this.mana+=this.speed
  16. },
  17. getdata:function(){
  18. axios
  19. .get('http://localhost:8888')
  20. .then(response => (this.name = response.data.name,
  21. this.mana = response.data.mana ))
  22. .catch(function (error) { // 请求失败处理
  23. console.log(error);
  24. });
  25. },
  26. savedata:function(){
  27. axios
  28. .get('http://localhost:8888',
  29. {
  30. params: {
  31. req:'save',
  32. mana: this.mana,
  33. name:'萧炎'
  34. }
  35. }
  36. )
  37. // .then(response => (this.name = response.data.name,
  38. // this.mana = response.data.mana ))
  39. // .catch(function (error) { // 请求失败处理
  40. // console.log(error);
  41. // });
  42. }
  43. },
  44. mounted:function(){
  45. setInterval(this.gradeup, 1000);
  46. }
  47. })
  48. </script>

2、后台服务器代码

首先要安装node.js,然后用node.js启动即可

  1. var express = require('express');
  2. var path = require('path');
  3. var fs = require('fs');
  4. var bp = require('body-parser');
  5. var app = express();
  6. app.use(bp.urlencoded({ extended: false }));
  7. //跨域
  8. app.all('*', (req, res, next) => {
  9. res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  10. res.header('Access-Control-Allow-Origin', '*');
  11. res.header('Access-Control-Allow-Metheds', 'PUT, POST, GET, DELETE, OPTIONS');
  12. res.header('X-Powered-By', 'nodejs');
  13. res.header('Content-Type', 'application/json;charset=utf-8');
  14. next();
  15. });
  16. //创建get接口
  17. app.get('', (req, res) => {
  18. console.log(req.query); //获取请求参数
  19. var filepath = 'C:\\Users\\wdx\\Desktop\\java+html\\vue\\data.json'; //路径
  20. //读取json文件
  21. fs.readFile(filepath, 'utf-8', function(err, data) {
  22. if (err) {
  23. res.send('文件读取失败');
  24. } else {
  25. res.send(data);
  26. if(req.query.req=='save'){
  27. console.log("保存");
  28. var person = data.toString();
  29. person = JSON.parse(person);
  30. console.log(person);
  31. person.mana=parseInt(req.query.mana);
  32. console.log(req.query.mana);
  33. console.log(person);
  34. var str = JSON.stringify(person);
  35. fs.writeFile('C:\\Users\\wdx\\Desktop\\java+html\\vue\\data.json',str,function(err){
  36. if(err){
  37. console.error(err);
  38. }
  39. console.log('存档成功');
  40. })
  41. }
  42. }
  43. });
  44. });
  45. var host = 'localhost'; //ip
  46. var port = 8888; //端口
  47. app.listen(port, host, () => {
  48. console.log(`http://localhost:8888`);
  49. });

 

3、json文件

{"name":"萧炎","power":100,"bleed":6000,"mana":1241,"grade":"斗之气","star":1}

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/104431
推荐阅读
相关标签
  

闽ICP备14008679号