简体   繁体   中英

Failed to load resource: the server responded with a status of 404 error after deploying project

I built a NodeJS/Angular project for a buddy of mine. Everything works fine locally, but once I made the project live, I got an error. The website is chestnutdetailing.com, and if you try to set an appointment you can duplicate the error i'm getting. I believe the error is with my server.js or app.js file. I'm using Express. When it tries to POST to /contact, I get a 404 error, pictured below. error message .

Here is my server.js code

 console.log("made it in the server file"); var express = require('express'); var mysql = require('mysql'); var bodyParser = require('body-parser'); var nodemailer = require('nodemailer'); var app = express(); app.use(bodyParser.json({})); app.listen(8080, function () { console.log("Listening on port 8080.....") }); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }); var connection = mysql.createConnection({ host: 'db694694191.db.1and1.com', port:3306, user: '.....', password: '.....', database: 'db694694191' }); connection.connect(); app.post('/contact', function(req, res, next){ //console.log(req.body); var appointment = req.body; var query = connection.query('insert into appointments set ?', appointment, function (err, result) { if (err) { console.error(err); return res.send(err); } else { return res.send('Ok'); } })}); app.get('/contact', function(req, res){ var query = connection.query('select * from appointments order by id desc limit 1;',function(err,rows){ if(err) { consol.error(err); return res.send(err); } else { console.log(); var transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '...', pass: '.....' } }); var mailOptions = { from: '...', to: '...', subject: 'Detailing Appointment Request - ' + rows[0].name, html: '<p><i>You have a new detailing appointment request. Please respond to this customer within 24 hours. </i></p><h2>Customer Information</h2><p><b>Name</b>: ' + rows[0].name + '</p> <p><b>Phone</b>: ' + rows[0].number + '</p> <p><b>Email</b>: ' + rows[0].email + '</p> <h2>Order Information</h2> <p><b>Request ID</b>: ' + rows[0].id + '</p> <p><b>Date/Time</b>: ' + rows[0].date + ' at ' + rows[0].time + '</p> <p><b>Service</b>: 1 ' + rows[0].vehicle_type + ', ' + rows[0].service + '</p> <p><b>Additional Services</b>: ' + rows[0].add_on + '</p>' }; transporter.sendMail(mailOptions, function (error, info) { if (error) { console.log(error); } else { console.log('Email sent: ' + info.response); } }); return res.send(rows); } })}); 

and here is the relevant portion of my app.js file:

 //Database connection $scope.submit= function(){ console.log('clicked submit'); var appointment = { name: $scope.name, number : $scope.number, email : $scope.email, vehicle_type: $scope.generateModel.vehicleType, service: $scope.generateModel.service, add_on: $scope.generateModel.addOnService, date: $scope.moment($scope.dt).format('MM/DD/YYYY'), time: $scope.moment($scope.mytime).format('hh:mm A'), } $http({ url: 'http://chestnutdetailing.com/contact', method: 'POST', data: appointment }).then(function() { $scope.getAppointments(); }) }; $scope.getAppointments = function() { $http({ url: 'http://chestnutdetailing.com/contact', method: 'GET' }).then(function(response) { $scope.lastAppointment = response; $scope.openAppointmentModal($scope.lastAppointment); }) } 

You web app is currently deployed on shared host using the Apache web server. Your NodeJS/Express server code is not being used. For this to work the way you expect, you'll need to deploy to a service that will execute you NodeJS code. I recommend you go through this tutorial to publish your app to Google's Cloud.

http://labs.roitraining.com/labs/gcp-node-express-cloud-functions/index.html#0

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