Node.js+Express+MySQL搭建React Native食品列表接口无响应问题
Hey there! Let’s work through this "Could not get any response" issue you’re hitting with your Express/MySQL backend for React Native. Here’s how to troubleshoot and fix it step by step:
1. Make Sure Your Express Server is Actually Running
First off, if your server isn’t listening on a port, Postman can’t connect at all. Double-check that you’ve added this critical code to start your Express app:
const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server up and running on port ${PORT}!`); });
After starting your server, watch your terminal—you should see that success message. If not, there’s an error stopping the server from booting (check for typos in your require statements or variable definitions).
2. Fix MySQL Connection Issues (They’re Probably Blocking Requests)
Your con() function for MySQL connections might be hanging if there’s a configuration error or unhandled connection failure. Add error handling to avoid getting stuck:
function con() { const connection = mysql.createConnection({ host: 'localhost', // Confirm this matches your MySQL host user: 'your_mysql_username', // Replace with your actual username password: 'your_mysql_password', // Replace with your actual password database: 'your_food_db' // Replace with your database name }); connection.connect((err) => { if (err) { console.error('Oops, failed to connect to MySQL:', err); throw err; // Or handle the error gracefully instead of crashing } console.log('Connected to MySQL successfully!'); }); return connection; }
Bad credentials or a wrong host will freeze the connection, which in turn freezes your API request—leading to Postman’s "no response" error.
3. Ensure Your Route Returns a Response & is Properly Mounted
Your router won’t work unless you mount it to your Express app. Add this line to link your router to a base path (like /api):
app.use('/api', router);
Also, every route handler must send a response to the client—otherwise the request will hang forever. Here’s an example of a working food list route that returns an object array:
router.get('/foods', (req, res) => { const dbConnection = con(); const getFoodsQuery = 'SELECT * FROM foods'; // Use your actual table name dbConnection.query(getFoodsQuery, (err, results) => { if (err) { console.error('Failed to fetch foods:', err); return res.status(500).json({ error: 'Could not load food list' }); } // Convert MySQL results to your desired object array structure const foodList = results.map(food => ({ id: food.id, name: food.food_name, description: food.food_description, price: food.price // Add other fields matching your database schema })); res.json(foodList); // Send the object array back to the client dbConnection.end(); // Clean up the MySQL connection to avoid leaks }); });
4. Double-Check Your Postman Request Setup
- Confirm your request URL matches your server’s port and route: e.g.,
http://localhost:3000/api/foods - Make sure you’re using the correct HTTP method (GET for the example route above)
- Check if your local firewall is blocking the port (3000 by default) or if you’re using a conflicting proxy
5. Add Essential Express Middleware
Don’t forget to add this middleware to parse JSON requests (it helps with compatibility even for GET requests):
app.use(express.json());
内容的提问来源于stack exchange,提问作者Allaude Bluesky

