我创建了一个网站,前端使用React,后端使用nodejs express。然而,当我尝试从前端调用其中一个后端函数时,请求需要2分钟,然后在控制台中返回此错误:
POST http://evogenesis.co.uk:3000/nodeapp/api/register net::ERR_EMPTY_RESPONSE
(anonymous)
script.js:17
react_devtools_backend.js:2655 Error: TypeError: Failed to fetch
at HTMLButtonElement.<anonymous> (<anonymous>:24:3)
这是我的script.js文件中的前端函数:
// Register new user and save todo list to database
registerButton.addEventListener('click', function() {
const email = emailInputRegister.value;
currentuseremail = email;
const todoList = [];
const data = { email: email, todoList: todoList };
fetch('http://evogenesis.co.uk:3000/nodeapp/api/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log('Success:', data);
})
.catch(error => {
console.error('Error:', error);
console.log(data);
});
});
它调用另一个名为app.py的文件中的后端函数。它位于一个名为nodeapp的文件夹中,该文件夹与script.py(public_html目录)在同一个目录中。下面是整个app.js文件:
const express = require('express');
const mysql = require('mysql');
const cors = require('cors');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const port = 3000;
// Create connection to MySQL database
const connection = mysql.createConnection({
host: '127.0.0.1',
user: 'evogenes_shbc',
password: 'mitsMysq27',
database: 'evogenes_Users'
});
// Use bodyParser middleware to parse JSON data
app.use(bodyParser.json());
app.use(cors)
// Serve static files from public directory
app.use(express.static(path.join(__dirname, '/')));
// Register new user and save todo list to database
app.post('/api/register', function() {
const email = req.body.email;
console.log(email);
const todoList = JSON.stringify(req.body.todoList);
console.log(todoList);
const query = `INSERT INTO users (username, todo_list) VALUES (?, ?)`;
console.log(query);
connection.query(query, [email, todoList], function(error) {
if (error) {
console.error('Error registering user:', error);
res.status(500).json({ error: error.message });
} else {
res.sendStatus(200);
}
});
console.log("query done");
});
app.post('/api/save', function(req, res) {
console.log("saving")
const email = req.body.email;
const todoList = JSON.stringify(req.body.todoList);
const query = `UPDATE users SET todo_list = ? WHERE username = ?`;
console.log(email);
connection.query(query, [todoList, email], function(error) {
if (error) {
console.error('Error registering user:', error);
res.sendStatus(500);
} else {
res.sendStatus(200);
}
});
});
// Start server
app.listen(port, function() {
console.log(`Server listening on port sdfsd`);
});
整个事情是运行在一个Node.js应用程序,我通过我的网络托管提供商(ifastnet)设置。我已经通过提供的GUI启动了应用程序,但我想我也可以尝试通过他们提供的终端运行它,但当我在nodeapp目录中运行node app.js时,我得到了这个错误:
events.js:160
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:1020:11)
at exports._exceptionWithHostPort (util.js:1043:20)
at Server._listen2 (net.js:1271:14)
at listen (net.js:1307:10)
at Server.listen (net.js:1403:5)
at EventEmitter.listen (/home/evogenes/nodevenv/public_html/nodeapp/10/lib/node_modules/express/lib/application.js:635:24)
at Object.<anonymous> (/home/evogenes/public_html/nodeapp/app.js:64:5)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
当我尝试通过postman向http://evogenesis.co.uk/nodeapp/api/register地址发出请求时,它会抛出以下错误:
{
"status": "error",
"message": "Incomplete response received from application"
}
我试过检查错误日志(据我所知,node.js应用的ifastnet错误消息会发送到您指定的日志文件中。然而,日志文件只包含以下内容:
App 11205 output: Server listening on port sdfsd
App 8735 output: Server listening on port sdfsd
App 31095 output: Server listening on port sdfsd
App 10462 output: Server listening on port sdfsd
App 5055 output: Server listening on port sdfsd
同样,没有新记录被添加到mysql数据库的表中(也由ifastnet提供)。为什么会出现这个问题,我如何解决它?我可以提供HTML页面的完整代码,如果需要的话,可以提供JavaScript前端的其余代码。
1条答案
按热度按时间0wi1tuuw1#
listenEADDRINUSE:::3000这个错误意味着你的端口已经被其他进程使用了,所以只要在app.listen()中将你的端口更改为其他值,例如8080