iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 9
0
自我挑戰組

30 天 node.js 學習筆記系列 第 9

Day 9 CORS 跨域存取


app.use((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Origin-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');

    if (req.method === 'OPTIONS') {
        res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE');
        return res.status(200).json({});
    }
    // if you don't call next() node api flow will stop this
    // because app.use is middleware, it will do something after action, like pipeline
    // So Don't call next() just like stop water flow here.
    // So that it will cause api pedding, and then timeout.
    next();
});

上一篇
Day 8 Router 錯誤處理
下一篇
Day 10 body-parse
系列文
30 天 node.js 學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言