加载资源失败:部署项目后服务器响应状态为404错误
Failed to load resource: the server responded with a status of 404 error after deploying project
我为我的一个好友构建了一个 NodeJS/Angular 项目。在本地一切正常,但是一旦我使项目上线,我就出错了。该网站是 chestnutdetailing.com,如果您尝试设置约会,您可能会重复我遇到的错误。我认为错误出在我的 server.js 或 app.js 文件中。我正在使用快递。当它尝试 POST 到 /contact 时,我收到 404 错误,如下图所示。 error message。
这是我的 server.js 代码
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);
}
})});
这里是我的 app.js 文件的相关部分:
//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);
})
}
您的网络应用程序当前部署在使用 Apache 网络服务器的共享主机上。您的 NodeJS/Express 服务器代码未被使用。为了使其按您期望的方式工作,您需要部署到将执行您的 NodeJS 代码的服务。我建议您通过本教程将您的应用程序发布到 Google 的云端。
http://labs.roitraining.com/labs/gcp-node-express-cloud-functions/index.html#0
我为我的一个好友构建了一个 NodeJS/Angular 项目。在本地一切正常,但是一旦我使项目上线,我就出错了。该网站是 chestnutdetailing.com,如果您尝试设置约会,您可能会重复我遇到的错误。我认为错误出在我的 server.js 或 app.js 文件中。我正在使用快递。当它尝试 POST 到 /contact 时,我收到 404 错误,如下图所示。 error message。
这是我的 server.js 代码
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);
}
})});
这里是我的 app.js 文件的相关部分:
//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);
})
}
您的网络应用程序当前部署在使用 Apache 网络服务器的共享主机上。您的 NodeJS/Express 服务器代码未被使用。为了使其按您期望的方式工作,您需要部署到将执行您的 NodeJS 代码的服务。我建议您通过本教程将您的应用程序发布到 Google 的云端。
http://labs.roitraining.com/labs/gcp-node-express-cloud-functions/index.html#0