LoginSignup
0
0

More than 1 year has passed since last update.

Express の CORS 対応

Last updated at Posted at 2022-04-17

こちらで作成したサーバーを CORS に対応させます。

必要なライブラリーのインストール

sudo npm install -g cors

フォルダー構造

$ tree
.
├── app.js
└── routes
    └── index.js

app.js のみ書き換えます。

app.js
//-------------------------------------------------------------------------
//	app.js
//
//					Apr/17/2022
//-------------------------------------------------------------------------
'use strict'

const express = require('express')
const routes = require('./routes')
const bodyParser = require("body-parser")
const cfenv = require('cfenv')
const cors = require('cors')		// 追加

var app = express()
app.use(cors())		// 追加
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

app.use(express.static(__dirname + '/public'))

const appEnv = cfenv.getAppEnv()

app.post('/post_test',routes.post_test)

app.listen(appEnv.port, '0.0.0.0', function() {
  console.log("server starting on " + appEnv.url)
})

//-------------------------------------------------------------------------
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0