简体   繁体   中英

How can I run a node.js server next to the current PSK setup?

I need to listen to POST requests on the /mail path toso that I can send e-mails via JS, without refreshing the page. I was thinking about an express nodejs server which will send the email when a POST request is made on /mail .

The gulp serve task currently uses browserSync to display the application, but this only displays the static part of the site. If I run gulp serve and then I run the node mail.js script which contains an express server, I get an EADDRINUSE error, because the express server is in conflict with browserSync, I think.

What can I do to overcome this error?

In your mail.js file, change the port number from 3000 to anything else. like 4000 (here app is your express instance)

 app.listen(3000, function( ....

to app.listen(4000, function( ....

This may result in CORS error when you post to ?mail now.

So you now need to add in your express server code add allowance for POST requests from localhost:3000 app by using something like below

app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "localhost:4000");
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Methods', 'POST');
    res.header('Access-Control-Allow-Headers', '*');
    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
        res.sendStatus(200);
    } else {
        next();
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM