Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Angular 6和Nodejs:请求的资源上不存在“Access Control Allow Origin”标头_Node.js_Angular_Typescript_Express_Bootstrap 4 - Fatal编程技术网

Node.js Angular 6和Nodejs:请求的资源上不存在“Access Control Allow Origin”标头

Node.js Angular 6和Nodejs:请求的资源上不存在“Access Control Allow Origin”标头,node.js,angular,typescript,express,bootstrap-4,Node.js,Angular,Typescript,Express,Bootstrap 4,我在angular 6应用程序和nodejs中有表单,提交表单时出现以下错误: 加载失败http://localhost:3000/contact/send: 对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。起源'http://localhost:4200因此,不允许访问。 我搜索并检查了其他类似的错误,但我无法摆脱错误: 这里是server.js // server.js const express = require('express');

我在angular 6应用程序和nodejs中有表单,提交表单时出现以下错误:

加载失败http://localhost:3000/contact/send: 对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。起源'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);
      });
这是node mailer的routes contact.js

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;
注意:我已经通过npm安装了cors,并尝试了其他方法,但没有

我的代码中缺少了什么?

Fix 只需确保cors飞行前已使用选项启用,并且在呼叫前已启用标头:

router.options('/send', cors()); // ADDED 
router.get('/send', cors(), (req, res) => { // ADDED
修理 只需确保cors飞行前已使用选项启用,并且在呼叫前已启用标头:

router.options('/send', cors()); // ADDED 
router.get('/send', cors(), (req, res) => { // ADDED

移动你的app.usecors;在应用程序上方。使用“/contact”,contact

移动你的app.usecors;在应用程序上方。使用“/contact”,contact

更改我获取此错误的选项http://localhost:3000/contact/send 0仍然相同,可能是我可以向您发送回购协议,您可以检查它:更改我获取此错误选项http://localhost:3000/contact/send 0仍然相同,可能我可以向您发送回购协议,您可以检查它:此外,如果您希望对CORS访问进行更精细的控制,移动'const cors=需要'cors';进入contact.js文件,并将其用作中间件。它将只适用于/contact/*routes then.hii Jeremy,在倒角之后,我仍然会得到此错误选项https://localhost:3000/contact/send 0您知道此错误吗?cors npm文档中的此部分应该会有所帮助。注意:这已在前面的回答中说明。谢谢,我很累,我将尝试其他方法,解决此问题,但得到其他错误:选项0,如果您希望对CORS访问进行更精细的控制,请移动“const CORS=require'CORS”;进入contact.js文件,并将其用作中间件。它将只适用于/contact/*routes then.hii Jeremy,在倒角之后,我仍然会得到此错误选项https://localhost:3000/contact/send 0您知道此错误吗?cors npm文档中的此部分应该会有所帮助。注意:这是在前面的回答中说的谢谢我累了,我将尝试其他方法,解决了此问题,但得到了其他错误:选项0