I have a NodeJS and React application made with a docker-compose.yml and I'm trying to put my application on port :8090, but when I do a curl, I get this error
curl localhost:8090
curl: (56) Recv failure: Connection reset by peer
docker-compose.yml
version: '3.1'
services:
web:
build:
context: .
args:
# Variaveis do frontend
- BACKEND_URL=http://localhost:8090
restart: always
container_name: petcertificados
ports:
- "8090:8080"
volumes:
- ./certificados:/usr/app/server/certificados
If I go inside my container, I can access my website, but not if I'm outside
[root#blastoise certificados]# docker exec -it petcertificados /bin/bash
root#a0d690c11c38:/usr/app/server# curl localhost:8080/api
<!DOCTYPE html>
<html>
<head>
<title>Certificados - Backend</title>
</head>
<body>
<h3>Certificados - BackEnd</h3>
</body>
Because in my personal computer this works, I think it may be a error with the docker in the machine. The docker version is Docker version 19.03.13, build 4484c46d9d and I'm in a CentOS. I tried to restart the docker with service docker restart but doesn't work and I don't have ideia of what could be.
[EDIT 1] My index.js where I set up the port configuration and start the application
const express = require('express')
const app = express();
const path = require('path');
const cors = require('cors')
const { BACKEND_PORT } = require("./constants/index.js");
const CLIENT_BUILD_PATH = path.join(__dirname, '../../frontend/build');
app.use(express.static(CLIENT_BUILD_PATH));
app.use(express.json());
app.use(cors());
app.use((req, res, next) => {
next();
})
app.get('/api', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'index.html'));
});
app.use('/api/certificados', require('./routes/Certificados'));
app.use('/api/eventos', require('./routes/Eventos'));
app.listen(BACKEND_PORT, () => {
console.info(`Iniciando PETCertificados!`)
});
I tried to put "0.0.0.0" but this is already the default on NodeJS
[EDIT 2]
Dockerfile
FROM node:14.5.0 as frontend
WORKDIR /usr/app/frontend
COPY frontend/package*.json ./
RUN npm install -qy
COPY frontend/ ./
ARG BACKEND_URL
ENV REACT_APP_BACKEND_URL ${BACKEND_URL}
RUN npm run build
FROM node:14.5.0
WORKDIR /usr/app/
COPY --from=frontend /usr/app/frontend/build ./frontend/build
WORKDIR /usr/app/server/
COPY backend/package*.json ./
RUN npm install -qy
COPY backend/ ./
CMD ["npm", "start"]
With a slightly modified Dockerfile / app.js, I am able to curl localhost:8090 and get a response.
Dockerfile:
FROM node:14-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "app.js" ]
App.js
const express = require('express')
const app = express();
const path = require('path');
const cors = require('cors')
const CLIENT_BUILD_PATH = path.join(__dirname, '../../frontend/build');
app.use(express.static(CLIENT_BUILD_PATH));
app.use(express.json());
app.use(cors());
app.use((req, res, next) => {
next();
})
app.get('/', (req, res) => {
res.send('Hello world!')
});
app.listen(8080, '0.0.0.0', () => {
console.info(`Iniciando PETCertificados!`)
});
docker-compose.yml stays the same.
Related
Following is my code -
apps.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/api/users', (_req, res) => {
res.send('Hello World');
})
app.listen(PORT, () => {
console.log(`Server running on port: `, PORT);
});
.env file
PORT=8000
Now when I run the program though terminal via command - node app.js
I am getting -
Server running on port: 3000
but I want it to run on 8000 and pick it from .env file. Let me know what I am doing wrong here.
I know while running from terminal I can define PORT=8000 or app.set() but I am looking to pick it from an environment file. Let me know what I am doing wrong here / in terms of understanding.
You can use dotenv npm package for custom environment variables.
Usage
Create a .env file in the root of your project:
PORT=8000
As early as possible in your application, import and configure dotenv:
require('dotenv').config();
// Your .env variables is now available in process.env object
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/api/users', (_req, res) => {
res.send('Hello World');
})
app.listen(PORT, () => {
console.log(`Server running on port: `, PORT);
});
Read more in the official package: dotenv
I've been messing around with codesphere lately and have a weird issue, at least to me:
my server.js:
const express =require('express');
const app = express();
const port = 8000;
app.get('/', (req,res) => {
res.send('Hi there')
});
app.listen(port, () => {
console.log("Running")
});
Upon npm start :
user#codesphere:app [master] $ npm start
personal_website#1.0.0 start /home/user/app
node server.js
then the output exits the server and returns to CLI
Output for running node server.js:
Another edit:
upper part, my package.json in their ide, lower part: package.json open in terminal (nano)
Now I actually think, it is save to assume they have trouble!
I'm deploying a project on my hosting, but I've some trouble with the 'entry point' of my app.
I've developed an application under react js (with webpack).
When I setup it, I don't know which file to make the 'Application startup file'?
For the moment, it's a simple 'server.js' that say hello and give me the current version of node.
When I'm on my project in local, I just launch npm start and it works.
Resolved by editing the 'server.js' file like this :
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'dist')));
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'), function(err) {
if (err) {
res.status(500).send(err)
}
})
});
app.listen(9000);
Hi I am currently trying to learn how to create a backend for my app but when trying to create a port 3000 and having curl it produces this error:
NodeTutorial git:(master) ✗ curl -v http://localhost:3000 - the connection is refused.
Please Help.
My code in my index.js is as follows:
var http = require('http'),
express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);
app.get('/', function (req, res) {
res.send('<html><body><h1>Hello World</h1></body></html>');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Seems your server has not started, try running below steps and see if you are able to connect or not:
Step 1: Create a directory which will contain all the files related to our app and execute npm init:
$ mkdir nodejs-server
$ npm init
Step 2: Install Express as a dependency:
$ cd nodejs-server
$ npm install --save express
Step 3: Create default entry point for Node.js i.e index.js, inside the same directory we created:
$ cd nodejs-server
$ touch index.js
Copy the content you have shown in your question in index.js:
var http = require('http'),
express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);
app.get('/', function (req, res) {
res.send('<html><body><h1>Hello World</h1></body></html>');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
Step 4: Move to directory nodejs-server and start the app following below command:
$ cd nodejs-server
$ node index.js
Here are the dependencies of my package.json file where i've added "cool-ascii-faces. I then need to update my index.js file to GET the /cool page so that on each reload I would see an ascii face. I'm getting a 404 error and it says 'Cannot GET /cool'
"dependencies": {
"ejs": "2.3.3",
"express": "4.13.3",
"cool-ascii-faces": "~1.3"
}
Below is my index.js file that calls declares cool
var cool = require('cool-ascii-faces');
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/cool', function(request, response) {
response.render('pages/index')
});
app.get('/cool', function(request, response) {
response.send(cool());
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
I then run npm install to update the dependencies and then heroku local, but get the 404 error.
Any help in the right direction would be great!
You're probably getting an exception when you start the node web server, due to a module dependency error.
Check your command/terminal window. If you see a red warning message pointing to your module.js file, you have an exception:
$ heroku local
forego | starting web.1 on port 5000
web.1 | module.js:341
In this case, you need to install the cool-ascii-faces module. In your 'node-js-getting-started' directory, use the following npm command to install:
$ npm i -S cool-ascii-faces
Also... you'll want to convert your index page route back to '/'. Your routes logic should look like this:
app.get('/', function(request, response) {
response.render('pages/index')
});
app.get('/cool', function(request, response) {
response.send(cool());
});
Otherwise, you'll always get the default 'pages/index' page when you hit the '/cool' route instead of a smiley face.
You don't have to include
app.set('port', (process.env.PORT || 5000));
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
}
Heroku will run "npm start" to start your server and dynamically choose the port. You don't have to specify port explicitly.
this problem happened for me. After I typed "git push heroku master", I typed "heroku open" and it worked.