Can't deploy Node restify app to AWS EB - node.js

I'm trying to deploy a basic restify node app to AWS EB but when I do I get a 502 Bad Gateway error. The AWS console also shows that the application is in Health: Severe. It seems as though it isn't correctly serving via port 80. Here is my server.js:
var restify = require('restify');
var server = restify.createServer();
server.get('/', function(req, res) {
res.send("Hello");
return next();
});
var port = process.env.PORT || 3000;
server.listen(port);

Seems as though on EB that nginx redirects to node on port 8081, so I changed to that.
Additionally for whatever reason the node instance wasn't being run after "eb deploy". I added "node server.js" in software configuration on aws and now it seems to work fine.

Related

Elastic Beanstalk Problem: Connection timing out when running my Node.js Express server

I'm trying to deploy my MERN app on Elastic Beanstalk, and I seem to be running into a final problem that I just cannot solve.
My app works fine when running my server locally (running node server), but when running on elastic beanstalk, the page never loads.
Upon inspection, the static elements are not being loaded, as seen in Dev Tools:
Image showing ERR_CONNECTION_TIMED_OUT in dev tools
I checked all the EB logs and did not find any errors or helpful messages.
I'm thinking the problem is with EB not being able to find my static files somehow. It should however, my build files are not ignored by git and are deployed to EB.
Here's some background about my project:
My backend and client code are in one project, with the following structure:
project
server.js
frontend
build
static
index.html
I run my app by building the react site, then running "node server" which runs great
Here is the relevent code from my server.js :
const port = process.env.PORT || 8081;
app.use(express.static(path.join(__dirname, 'frontend/build')));
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'frontend/build/index.html'));
});
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
The server is successfully sending logs that the server is running and that the database has established a connection. So it seems the server is fine, it's just that the front-end is the problem.
eb config file:
option_settings:
aws:elasticbeanstalk:container:nodejs:
NodeCommand: "npm start"
aws:elasticbeanstalk:application:environment:
PORT: 8081
NODE_ENV: production
aws:elasticbeanstalk:container:nodejs:staticfiles:
/static: /frontend/build/static
I'm at a loss on how to solve this. The EB was deployed through the CLI and I haven't messed with any settings. I'm letting EB know where my static files are, and I believe it would say not found, rather than timing out.
Any help would be appreciated
Solved.
The problem was with using Helmet in my express server. I had ommited the code, thinking it not relevant, but here is the top portion of server.js, with the last line being the relevant portion:
const AWS = require('aws-sdk');
const cors = require('cors');
const express = require('express');
const helmet = require('helmet');
const mongoose = require('mongoose');
const path = require('path');
let Download = require('./models/Download.js');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 8081;
app.use(helmet());
Not using helmet solves the issue.
To be honest, I'm not sure why this is the problem.
I assume that the problem is that helmet provides some security that my bare bones EB simply is not providing.
EDIT: Specifically, the problem is with CSP. Setting contentSecurityPolicy to false in Helmet is enough to fix the issue.

CRA Socket.io request returns net::ERR_CONNECTION_TIMED_OUT

Hello. I've spent some time without luck trying to understand the problem here.
I've looked through each Question on StackOverflow which seems to deal with the same problem, though nothing has worked so far.
I have a simple chat app built using Create React App and Socket.io (which runs fine on localhost), but when deployed to my Node server I'm receiving ERR_CONNECTION_TIMED_OUT errors and no response. The website itself runs fine, but when I make a call to my Socket.io server, but errors.
I'm guessing this is down to my lack of knowledge with how Node and Socket.io want to work.
Some info:
server.js
const path = require("path");
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const port = 8080;
http.listen(port, () => console.log(`http: Listening on port ${port}`));
const io = require("socket.io")(http, { cookie: false });
app.use(express.static(path.join(__dirname, "build")));
app.get("/*", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
io.on("connection", (socket) => {
console.log("New client connected");
// Emitting a new message. Will be consumed by the client
socket.on("messages", (data) => {
socket.broadcast.emit("messages", data);
});
//A special namespace "disconnect" for when a client disconnects
socket.on("disconnect", () => console.log("Client disconnected"));
});
client.js
....
const socket =
process.env.NODE_ENV === "development"
? io("http://localhost:4001")
: io("https://my-test-site:8080");
socket.on("messages", (msgs: string[]) => {
setMessages(msgs);
});
....
docker-compose.yml
version: "X.X"
services:
app:
image: "my-docker-image"
build:
context: .
dockerfile: Dockerfile
args:
DEPENDENCY: "my-deps"
ports:
- 8080:8080
Dockerfile
...
RUN yarn build
CMD node server.js // run my server.js
...
UPDATE: I got around this problem by making sure my main port was only used to run Express (with socket.io) - in my set up that was port: 8080. When running in the same Docker container, I don't think I needed to create and use the https version of the express 'createServer'.
This looks like you forgot to map the port of your docker container. The expose statement in your dockerfile will only advertise for other docker containers, which share a docker network with your container, that they can connect to port 4001 of your container.
The port mapping can be configured with the -p flag for docker run commands. In your case the full command look somehow like this:
docker run -p 4001:4001 your_image_name
Also, do you have a signed certificate? Browser will likely block the conneciton if they do not trust your servers certificate.
I got around this problem by keeping just one port available (in my case :8080). This port is what express/socket.io is using (originally I had two different ports, one for my site, one for express). Also, in my case, when running in the same Docker container, I didn't require the require("https").createServer(app) (https) version of the server, as http was sufficient.

Howto run google assistand bitcoinInfo example webhook index.js

I am trying to run the google assistant example webhook nodejs application(index.js) on my own server, but don't know what is the webhosting setup for this..
https://codelabs.developers.google.com/codelabs/your-first-action-on-google-with-webhook/#2
What is the environment to run this app on my server? Since it's not running as a listening server, I can't use nginx, node_cgi is not mature with apache, how am I supposed to run this sample?
Excellent point, and you should be sure to file a bug request on the page to indicate it is unclear.
The code, as presented, is meant to run using Google Cloud Functions.
This doesn't mean you can't run it on your own server - just that you need to know how to run a Node.js server outside of your Apache or Nginx environment. I've seen a number of configurations, but typically you'll have the Node.js server application running and listening to a local port and have a proxy between your externally facing web server at a particular path and this port.
But even that isn't sufficient in this case - the code itself doesn't listen on a port - it expects to be handed a request and response object in the form that Express.js with a JSON middleware can handle. To do that, you'll need to have installed the Express.js library and then start listening with code such as:
const express = require('express');
const app = express();
app.use( express.json() );
app.get('/', (req, res) => exports.bitcoinInfo( req, res ));
app.listen(3000, () => console.log('App listening on port 3000!'));
Thanks for the help to #Prisoner and #Ido Green link works even better! The minimum to run the sample I did the following:
Create a new nodejs project with mainfile main.js, install express and actions-on-google
mkdir googleActionServer
cd googleActionServer
npm init
npm install --save actions-on-google
npm install --save express`
Copy the index.js from google and put this into main.js
const express = require('express');
const bitcoinInfo = require("./index");
const app = express();
app.use( express.json() );
app.post('/', (req, res) => bitcoinInfo.bitcoinInfo( req, res ));
app.listen(3000, () => console.log('App listening on port 3000!'));
Start the application by running:
node ./main.js
To test with DialogFlow, download and install ngrok to /usr/local/bin for ex and then run:
ngrok http 3000
Ngrok will give you an url that is accesible from outside and forward the requests to the nodejs app. It will also create a https for you, so copy paste the https address into DialogFlow webhook address and you are set to go

Upload react basic application to server

I am trying to upload the following to my personal server to see how it works:
https://github.com/remarkablemark/universal-react-tutorial
I have tried to change the port here: (server.js)
require('babel-register')({
presets: ['react']
});
var express = require('express');
var app = express();
app.use(express.static('public'));
app.use(require('./routes/index.jsx'));
var PORT = 80;
app.listen(PORT, function() {
console.log('http://localhost:' + PORT);
});
but when I type the corresponding url I get this:
**
Index of /ReactServer
Parent Directory
Component.jsx
client.js
public/
routes/
server.js
webpack.config.js
Apache Server at www.alessandrosantese.com Port 80
**
I can see the app working fine at http://localhost:3000/ but I would like to test it on the server (I have never deployed a react application on a live server)
This is more of deploying node.js to remote server.
I would recommend you to use heroku
Follow these steps to deploy your app easily to their servers.

NodeJS Deployment confusion between localhost and other domain?

I have a simple program which executes fine in localhost.
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var port = process.env.PORT || 8080;
var host = "127.0.0.1";
var server = app.listen(port, host, function(){
console.log("Server running in : ",host ," with port no : ",port);
});
Trying to deploy the same to heroku using codeship. Everything is building perfect except the last line of deployment test command i.e node index.js which in turn is referring to 127.0.0.1 and stops deploying. May i know do i need to change something here for the host and port address
Just don't provide a host:
var server = app.listen(port, function() {
console.log('Server listening on', port);
});
(This implies, "accept connections on any host, on this port", vs what you're trying which implies, "accept connections on 127.0.0.1 on this port")
Try to run your app on localhost with the help of foreman that is a part of the Heroku Toolbelt. For instance:
foreman start web
You should see your app running on http://localhost:5000 or the port you have specified in your package.json file.
Suggest this link for further queries:
prerequisites to deploy a node app on Heroku?
I was able to host it successfully following through this steps
As suggested by #hunterloftis, i removed hostname.
More importantly, Procfile was missing,so added it and deployed successfully

Resources