node.js - Angular 6 และ Nodejs: ไม่มีหัวข้อ 'Access-Control-Allow-Origin' บนทรัพยากรที่ร้องขอ
ฉันมีฟอร์มในแอป Angular 6 และ Node.js เมื่อฉันส่งฟอร์ม ฉันได้รับข้อผิดพลาดดังต่อไปนี้:
ไม่สามารถโหลด http://localhost:3000/contact/send: การตอบสนองต่อคำขอ preflight ไม่ผ่านการตรวจสอบการเข้าถึงควบคุม: ไม่มีหัวข้อ 'Access-Control-Allow-Origin' ในทรัพยากรที่ร้องขอ ดังนั้นต้นทาง 'http://localhost:4200' จึงไม่ได้รับอนุญาตให้เข้าถึง ฉันค้นหาและตรวจสอบข้อผิดพลาดที่คล้ายกันอื่น ๆ แต่ฉันไม่สามารถกำจัดข้อผิดพลาดนี้ได้ :(
นี่คือ server.js
// server.js
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const app = express();
// Port Number
const port = process.env.PORT || 3000
// Run the app by serving the static files
// in the dist directory
app.use(express.static(path.join(__dirname, '/majeni/dist/majeni')));
// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//routes
const contact = require('./app/routes/contact');
app.use('/contact', contact);
// CORS Middleware
app.use(cors());
// If an incoming request uses
// a protocol other than HTTPS,
// redirect that request to the
// same url but with HTTPS
const forceSSL = function () {
return function (req, res, next) {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(
['https://', req.get('Host'), req.url].join('')
);
}
next();
}
}
// Instruct the app
// to use the forceSSL
// middleware
app.use(forceSSL());
// For all GET requests, send back index.html
// so that PathLocationStrategy can be used
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname + '/majeni/dist/majeni/index.html'));
});
// Start Server
app.listen(port, () => {
console.log('Server started on port '+port);
});
และนี่คือเส้นทาง (contact.js สำหรับ node mailer)
const express = require('express');
const router = express.Router();
const request = require('request');
const nodemailer = require('nodemailer');
router.get('/send', (req, res) => {
const outputData = `
<p>You have a new contact request</p>
<h3>Contact Details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Email: ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: 'MY EMAIL',
pass: 'THE PASSWORD'
},
tls: {
rejectUnauthorized: false
}
});
let HelperOptions = {
from: '"MYNAME" <MYEMAIL,
to: 'MYEMAIL',
subject: 'Majeni Contact Request',
text: 'Hello',
html: outputData
};
transporter.sendMail(HelperOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log("The message was sent!");
console.log(info);
});
});
module.exports = router;
หมายเหตุ: ฉันได้ติดตั้ง cors ผ่าน npm และลองวิธีอื่นแล้วแต่ไม่ได้ผล
ฉันกำลังพลาดอะไรในโค้ดของฉัน?
คำตอบ #1
แก้ไข
แค่ตรวจสอบให้แน่ใจว่า cors preflight ถูกเปิดใช้งานด้วย options และ header ถูกเปิดใช้งานก่อนการเรียก:
router.options('/send', cors()); // ADDED
router.get('/send', cors(), (req, res) => { // ADDED
คำตอบ #2
ย้าย app.use(cors()); ของคุณขึ้นไปเหนือ app.use('/contact', contact);
