I receive an 'Unable to Connect' error in my browser when trying to connect to my Node Express application. At (my servers ip address) 1.1.1.1:5000. The application works fine in my development environment but not on my AWS EC2 Linux server.
curl: (7) Failed to connect to 1.1.1.1 port 5000: Connection refused
UPDATE
pm2 start app.js
I started it with npm start
and I the app is hosted at port 5000 successfully. API is running
fetch
api to call the backend at 127.0.0.1:5000 and receive a Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:5000/pickTicket/21780482. (Reason: CORS request did not succeed). 2 TypeError: NetworkError when attempting to fetch resource.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:5000/pickTicket/21780482. (Reason: CORS request did not succeed). 2 TypeError: NetworkError when attempting to fetch resource.
(*Note: My api is on the same server as my nginx/react app)` My application starts with app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');
var compression = require('compression');
var fetch = require('node-fetch');
var pickTicketRouter = require('./routes/pickTicket');
var kdlRouter = require('./routes/kdl')
console.log('Creating API')
var app = express();
app.use(cors());
app.options('*', cors());
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(compression());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('API is running\n');
});
app.use('/pickTicket', pickTicketRouter);
app.use('/kdl', kdlRouter)
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
/bin/www
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('api:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '5000');
console.log('Listening on port: ', port);
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
package.json
{
"name": "api",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon ./bin/www"
},
"dependencies": {
"compression": "^1.7.4",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"express": "~4.16.1",
"forever": "^1.0.0",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"morgan": "~1.9.1",
"mssql": "^5.1.0",
"node-fetch": "^2.6.0",
"sequelize": "^5.11.0",
"tedious": "^6.2.0"
},
"devDependencies": {
"nodemon": "^1.19.1"
}
}
I expect to see get a response from the api but instead got a CORS error.
I have a few questions regarding the different environments. Is your DEV environment hosted in AWS? If not, I would look at AWS Security Groups to make sure to have the correct TCP protocol for your application.
Also, did you deploy this EC2 into the default VPC or did you create your own VPC? If you have created a VPC, it could be a routing issue or network level issue.
我在app.js而不是bin / www上调用pm2 start
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.