This is my first try at Heroku. I was able to "deploy successful" on heroku, but when visiting my app, it says "application failed."
I followed this guide:
https://scotch.io/tutorials/use-mongodb-with-a-node-application-on-heroku
I think the tricky thing is because I'm using Mongoose, it may not relate well with the URI. I'm also using mlab addon.
Here's my setup for my backend:
var request = require('request');
var app = express();
var mongoCredentialss = require('/mongo_credentialss.json');
var conn = mongoose.connection;
var path = require('path');
// connect to the database
mongoose.connect('mongodb://' + mongoCredentials.username + ':' + mongoCredentialss.password + '#ds012345.mlab.com:12345/mydatabase-db');
mongoose.Promise = Promise;
app.listen(3000, function() {
console.log('Listening on 3000...');
});
I didn't really understand the .env, to make it work locally, I just stored my username and password in a hidden .json file thanks to .gitignore. From the guide though, I just tried embedding URI straight into my node file as a variable to see if that even works. I'm not too sure how 'hidden environments' work...
Later on when I started over, I also kept having a recurring problem with heroku not detecting its standard buildpacks:set heroku/nodejs
****EDIT:
Still seeing some issues. I took out my 'mongo creds' so my server.js file now looks like this:
var express = require('express');
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI);
var Promise = require('bluebird');
var bodyParser = require('body-parser');
var randtoken = require('rand-token');
var cors = require('cors');
var request = require('request');
var app = express();
var conn = mongoose.connection;
var fs = require('fs');
var path = require('path');
app.listen(3000, function() {
console.log('Our app is running on http://localhost:' + 3000);
});
Some further context: I have one main project folder, which houses 2 sub folders: backend and frontend. When I deploy it to heroku, I change directory into the main folder to deploy everything.
Also, if it helps, here's what my package.json looks like:
"dependencies": {
"bluebird": "^3.4.1",
"body-parser": "^1.15.2",
"cors": "^2.7.1",
"express": "^4.14.0",
"mongoose": "^4.5.3",
"my-bcrypt": "^1.0.2",
"rand-token": "^0.2.1",
"request": "^2.74.0"
},
"engines": {
"node": "==6.0.0"
}
}
******EDIT 2:
heroku logs can't find my 'backend.js'. Right now, I've got one main project folder directory, inside it is 2 folders: --->backend.js (node) and --->frontend.js (angular)
My package.json looks like this:
**"main": "frontend.js",**
"scripts": {
**"start": "node backend.js",**
"test": "echo \"Error: no test specified\" && exit 1"
},
I may be misunderstanding how heroku is finding my main .js files. From my understanding 'start' is for the node file, and the 'main' file is for the frontend stuff like angular/html stuff.
You should be able to connect Mongoose to your MLab database by doing this:
mongoose.connect(process.env.MONGODB_URI);
When you are using an addon on Heroku, the provider (MLab in this case) will create a database for you, and then store the connection details (the URL with the database host, username, password, port, etc.) as an environment variable that your application can use.
That's why you need to pass that environment variable process.env.MONGODB_URI into mongoose.connect: it will connect you to the right database =)
UPDATE: Since you updated your question, I noticed another issue that will prevent this from running. You need to tell your app to listen on a specific Heroku port (not 3000).
What you should do is modify your app.listen(3000) call to instead say: app.listen(process.env.PORT || 3000); This will force your application run correctly on both Heroku AND you local development box.
You require mongo using this line:
var mongoCredentialss = require('/mongo_credentialss.json');
but you've set your password in your mongoCredentials as:
mongoose.connect('mongodb://' + mongoCredentials.username + ':' + mongoCredentialss.password + '#ds012345.mlab.com:12345/mydatabase-db');
your username is bound to mongoCredentials.username rather than the mongoCredentialss.username
Simple typo in the code which is bound to lead to db connection errors.
Related
I am working on a project with Node.js, React.js and MongoDB.
When I send request to server, I get the following error:
Error occurred while trying to proxy request /api/auth/login from localhost:3000 to http://localhost:6000 (ECONNRESET).
I have my client running at port 3000, server at port 6000 locally. Here is the client side proxy middleware setup code:
const proxy = require("http-proxy-middleware");
module.exports = function(app) {
app.use(proxy("/api/", { target: "http://localhost:6000", "secure": "false" }));
};
I have tried using 127.0.0.1 inplace of localhost, but didn't work.
The project works fine in Windows laptop. But, it is having problem with M1 Mac.
Any guidance would be of great help to me.
I got the same error using M1.
This code started working correctly for me.
http://localhost:3000/ -> http://127.0.0.1:3000/
server.js
"use strict";
const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");
const PORT = 9090;
const HOST = "0.0.0.0";
const app = express();
app.use(
createProxyMiddleware("/", {
target: "http://127.0.0.1:3000/",
})
);
app.listen(PORT, HOST);
packege.json
{
"name": "web",
"version": "1.0.8",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.18.2",
"http-proxy-middleware": "^2.0.6"
}
}
node v18.11.0
npm 8.19.2
Server at "http://127.0.0.1:3000/" - default configuration for create-react-app ("react-scripts": "^5.0.1")
I changed the version of Node.js to 14.9.0 and it worked.
These are the solutions found in the internet that didn't work for me:
Changing node.js version to other stable version 16 or 18
specifying an IPv4 address like this on server (because I can see my server was running on IPv6): server.listen(13882, "0.0.0.0", function() { });
Removing proxy entry from the Package.json file
Updating to {target: "http://localhost:6000/"} OR {target: "https://localhost:6000/"} OR {target: "http://127.0.0.1:6000"} OR
{'http://[::1]:6000'} OR {app.use(proxy("/api/", {target:"http://localhost:6000",secure: false,changeOrigin: true}));}
I have this entry in package.json file "proxy": "http://localhost:6000"
This is my setupProxy.js file
const proxy = require("http-proxy-middleware");
module.exports = function(app) {
app.use(proxy("/api/", { target: "http://localhost:6000" }));
};
I'm using nodemon in my nodejs project because I want whenever I made any changes it will restart automatically everything works fine but now problem is I want to use a lib
which include puppeteer lib whenever I made any changes nodemon close the chromium browser and re-open it which take some time. This is making me slow in development. Is there any way I can stop this behaviour.
Here is my code.
const express = require("express");
const app = express();
const http = require("http");
const server = http.createServer(app);
const { Client } = require("whatsapp-web.js");
const client = new Client({ puppeteer: { headless: false } });
client.initialize();
console.log("changes 7");
server.listen(3000, () => {
console.log("listening on *:3000");
});
Whenever I made any changes it restart everything. I don't want to restart the client every time.
I don't know about nodemon, but if you can edit the library, you can re-use an existent browser.
Try, from node shell:
(await require('puppeteer').launch()).wsEndpoint()
this return a connection string that you can reuse.
And, then, you can connect with the created instace with connect api
Edit: your library allows ws socket! :-)
const client = new Client({
puppeteer: {
browserWSEndpoint: `ws://localhost:3000`
}
});
create nodemon.json in your project directory
nodemon will automatically look for this file and use it if exist
and write in nodemon.json
{
//list of directory you want to watch
"watch": ["./","server","someOtherDir"],
"ext": "js,ts,json", //file extension to watch
"ignore": ["ignoreThisDir","someDir/*.js"], //specify files or directory to ignore
// specify entry of the project
"exec" : "node app.js"
//another example for exec "exec": "ts-node --project tsconfig.server.json server/app.ts"
}
I followed the steps at:
https://devcenter.heroku.com/articles/getting-started-with-nodejs#introduction
When I ran heroku local it couldn't connect - I saw that it was using the process.env.DATABASE_URL and got it to my local .env file using:
https://devcenter.heroku.com/articles/heroku-local
But it still wouldn't connect, I've added a console.log to see the error:
"error: no pg_hba.conf entry for host "62.90.xxx.yyy", user "username", database "password", SSL off"
What now?
After a lot of searches it turns out that added 'pg.defaults.ssl = true' solves the problem for me while allowing me to stay as close as possible to the sample provided by Heroku.
Here is my code
const cool = require('cool-ascii-faces');
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
const pg = require('pg');
pg.defaults.ssl = true; //this is it!!!
express()
.use(express.static(path.join(__dirname, 'public')))
.set('views', path.join(__dirname, 'views'))
....
A neat solution would be enabling SSL only for the Heroku server connection. For that add dialectOptions.ssl=true to Heroku environment in config.json file:
{
"production": {
"use_env_variable": "DATABASE_URL",
"dialectOptions": { "ssl": true },
"logging": false
}
We'd like to surf the internet through a node.js proxy on appfog.
We tried this code:
proxy2a.js:
var http = require('http');
var port = process.env.VCAP_APP_PORT || 8080;
console.log ('the portnumber is: '+ port) ;
http.createServer(function(request, response) {
var proxy = http.createClient(80, "checkip.dyndns.org");
var proxy_request = proxy.request(request.method, request.url, request.headers);
proxy_request.on('response', function (proxy_response) {
proxy_response.pipe(response);
response.writeHead(proxy_response.statusCode, proxy_response.headers);
console.log(proxy_response.statusCode) ;
});
request.pipe(proxy_request);
}).listen(port);
package.json:
{
"name": "proxy2a",
"author": "parker",
"version": "0.0.0-14",
"dependencies": {
},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "0.6.x",
"iisnode": "*"
},
"scripts": {
"start": "node proxy2a.js"
}
}
We have the code from this question. (Actually we need the server only to access one website, so it is fine that this server only serves one.)
This code works great when we run the server on our local machine.(Use node version 0.6.x.)
But when we deploy to appfog, there is no reaction. (We get the ip by pinging the deploy url with another console.) Do you know how to get such a proxy server to work on appfog or any other node.js host?
Your code works exactly as-is for me. I copied your two files as-is, and ran the following commands--
af login
// enter my creds
af update testproxy
Then went to http://testproxy.aws.af.cm/ and it works. Are you sure you uploading your app correctly, are using the correct domain name, and that your app is started?
(VCAP_APP_PORT is set to 80, so your app will be running on port 80 due to this line: var port = process.env.VCAP_APP_PORT || 8080;)
Whenever I try to use require("socket.io"); on heroku it fails with the message "Cannot find module socket.io".
I think it’s an issue with my setup, because the same is running fine in my local node.js server.
What do I have to change?
Heroku on cedar does not support websockets
Anyway you can use socket.io with
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku
You have to change your PaaS provider. Heroku doesn't support websockets. Where as nodejitsu is known for support websockets.
Also, you might have forgot adding socket.io in package.json dependency lists.
package.json modified as
"dependencies": {
"async": "0.1.18",
"ejs": "0.4.3",
"express": "2.4.6",
"faceplate": "0.0.4",
"socket.io": "latest" },
And the serverside code is:
var port=process.env.PORT || 3000;
var http=require('http');
var app=http.createServer(function(req,res){
res.write("server listening to port:"+port);
res.end();
}).listen(port);
socket=require("socket.io");
io=socket.listen(app);
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.sockets.on("connection",function(socket){
console.log("new connection");
socket.on("eventA",function(data){
io.sockets.emit("eventB",data);
});
});
Working like a charm!!!
You need to change the transport option on socket.IO to xhr-polling with a (10) second duration, according to this project wiki page.
websocket transport is working on Heroku in beta state. you can enable it with heroku labs:enable websockets -a YOUR_APP_NAME
These are solutions to socket.io related problems
I hope i will work
Ports in your (index.js or server.js) & (index.html and your client.js) port must be different. (refer below code)
=============your index.js file ======================
(port here is 8000)
const express = require("express")
var app = express();
const http = require('http')
var server = http.createServer(app);
const port = process.env.PORT || 8000
server.listen(port,()=>
{
console.log("Listening at port => "+port)
});
var io = require('socket.io')(server, {
cors: {
origin: '*',
}
});
const cors = require("cors")
app.use(cors())
=============your client.js file ======================
port here is 8080
const socket = io.connect('https://localhost:8080/')
=============your index.html file ======================
port here is 8080
<script defer src="https://localhost:8080/socket.io/socket.io.js">
</script>
Remember your "server.js or index.js" port should be different from "client.js" port (Rememeber this is important)
(index.html and your client.js) port must be same
You should always use 'http' while working with socket.io (refer above code)
U may not included cors as it allows u to have more resourses , without cors heroku prevent some dependencies to not install in heroku (refer above code)
Try replacing "io" to "io.connect"
const socket = io.connect('https://localhost:8080/')
Must write tag at the end in the HTML
U may forget to add this code which is must in "socket.io"
It is required in your html file
delete "node_modules" and "package-lock.json"
and write "npm i" in cmd
This should be in package.json 's scripts
"start":"node index.js",
I am not talking about nodemon , use simple node over here
May be version is creating a problem , u can avoid it by copying all "devDependencies" to "dependencies" in "package.json" and put "*" in version like this
"dependencies": {
"cors": "*",
"express": "*",
"nodemon": "*",
"socket.io": "*"
},
"devDependencies": {}