【问题标题】:Is there a way to get local storage object in manifest.json?有没有办法在 manifest.json 中获取本地存储对象?
【发布时间】:2019-03-26 23:53:59
【问题描述】:

我有一个问题。我有一个要求,需要从本地存储中获取一些东西并将其设置为 manifest.json 中的短名称。我正在构建一个渐进式 Web 应用程序,让 manifest.json 配置其设置至关重要。你知道怎么做吗?我很乐意为您提供任何帮助。谢谢!

【问题讨论】:

  • 有很多方法可以做到这一点。请提供有关您的系统的更多详细信息,您在前端和后端使用了哪些技术?
  • @waghcwb 这是给 Nodejs/reactjs 的
  • 您使用任何模块捆绑器吗? (例如:webpack)
  • @waghcwb 是的,我确实使用 webpack :)

标签: javascript node.js reactjs manifest.json


【解决方案1】:

您可以在您的系统中使用您想要插入到manifest.json 的数据创建一个端点,在Express 中会是这样的(我不担心安全性)

server.js

const express = require('express')
const app = express()
const fs = require('fs')

app.use(express.json())

app.get('/', (req, res) => res.sendfile('./index.html'))

app.post('/', (req, res) => {
  fs.writeFileSync('./file.json', JSON.stringify({
    description: req.body.description
  }, null, 2), (err) => {
    if (err) throw err

    res.send(`The new description is: ${req.body.description}`)
  })
})

app.listen(3000, () => console.log('server is listening'))

client.js

(async() => {
  const result = await fetch('http://localhost:3000', {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },

    body: JSON.stringify({
      description: 'only for testing purposes'
    })
  })

  console.log(await result.json())
})()

【讨论】:

  • @bEtTyBarnes,它在fs.writeFileSync 行中。重命名'./file.json' 并使用您的manifest.json 的路径
  • 对不起,我错过了。顺便说一句,另外的问题是这是否会以编程方式添加到 标记内的 index.html 中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 2019-02-23
  • 2021-01-12
  • 2011-06-12
  • 2020-08-13
  • 1970-01-01
相关资源
最近更新 更多