提问者:小点点

错误:在以express发送邮件后无法设置邮件头。js应用程序


我在开快车。js应用程序,其中我有一个返回json对象的路由

https://serene-depths-3284.herokuapp.com/chapters?setLng=en-UK

在我的控制器文件夹中,我有章节。咖啡文件,如下所示:

# Return the chapters.json based on the language
i18n = require "i18next"
fs = require "fs"
cldr = require "cldr"
__ = require "underscore"

exports.chapters = (req, res, err) ->
    fs.readFile "./data/chapters.json", (err, chapterJSON) ->
        console.log("read file error", err) if err
        tzmChapters = JSON.parse chapterJSON
        try
            # ...
            lngCode = req.query.setLng.split("-")[0]
        catch e
            # fallback to user locale
            lngCode = i18n.lng().split("-")[1]
        allCountries = cldr.extractTerritoryDisplayNames(lngCode)
        tzmNetwork = []
        tzm = __.each tzmChapters, (value, index, list) ->
            locale = value.desc.LOCALES.split("-")[1]
            tzmNetwork.push({link:value.desc.WEBSITE,contact:value.desc.CONTACT,country: allCountries[locale]})
        res.json {tzmNetwork}

该代码可在以下网址找到:https://github.com/TZM/tzm-blade/blob/master/app/controllers/chapters.coffee

以及为什么我尝试从不同的站点访问章节json文件,我得到:

XMLHttpRequest cannot load http://localhost:3000/chapters. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.

我如何在我的代码中启用它,有没有更好的方法,因为我也在footer.blade文件中使用它,我也在我的apps.coffee文件中阅读chapters.json文件https://github . com/TZM/tzm-blade/blob/master/app/config/apps . coffee # L163

非常感谢任何建议。


共1个答案

匿名用户

首先阅读 CORS

然后为cors安装Express中间件