const app=require('./app')
const dotenv =require('dotenv');
const PORT=3000;
dotenv.config({path:'backend/config/config.env'})
app.listen(process.env.PORT,()=>{
console.log(`server Started on Port: ${process.env.PORT} in ${process.env.NODE_ENV}mode`);
})
const app=require('./app')
const dotenv =require('dotenv');
const PORT=3000;
dotenv.config({path:'backend/config/config.env'})
app.listen(process.env.PORT,()=>{
console.log(`server Started on Port: ${process.env.PORT} in ${process.env.NODE_ENV}mode`);
})
I am getting error as follows:
server Started on Port: undefined in undefined mode
You need to make a file on the root say local.env of your project and assign variable values in that.
PORT=3000
NODE_ENV=DEVELOPMENT
Related
I am trying to learn MERN and have hit an issue straight away
App.listen(process.env.PORT, () => {
console.log(`Server started on PORT: ${process.env.PORT} in ${process.env.NODE_ENV} mode.`)
})
returns "Server started on PORT: undefined in undefined mode."
Here is structure
backend:
config:
config.ev
App.js
server.js
App.js>
const express = require('express); const App = express(); module.exports = App
server.js> `const App = require('./App');
const dotenv = require('dotenv')
// Config set up
dotenv.config({ path: 'backend\config\config.env'})
App.listen(process.env.PORT, () => {
console.log(Server started on PORT: ${process.env.PORT} in ${process.env.NODE_ENV} mode.)
})`
config.env PORT = 4000 NODE_ENV = DEVELOPMENT
to require dotenv, use this instead of what you have currently
const dotenv = require("dotenv").config();
i also noticed some typos in your question like the missing quotation mark when you required express but i assume this was a typo from writing it on here and not in your actual code.
and you can find more info on dotenv here
I'm trying to create a simple backend api for an app I'm working on (create-react-app) and decided to use node express. I'm getting the error 'cannot GET' when I open my browser and I don't understand why.
Here is my server/index.js file:
const express = require("express");
const PORT = process.env.PORT || 3001;
const app = express();
app.get("/api", (req, res) => {
res.json({ message: "Hello from server!" });
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
Here is my start script:
"scripts": {
"start": "node index.js"
}
In my console I see this error:
Failed to load resource: the server responded with a status of 404 (Not Found)
This is my folder structure:
I have an app called 1REPMAX that contains the folders
>build
>node-modules
>public
>server
>node_modules
>index.js
>package-lock.json
>package.json
>src
.gitignore
package-locj.json
package.json
So what I've been doing is I cd into server and then run npm start.
When I open my browser to localhost:3001 I get the error. Any help on what I'm doing wrong? If more information is needed just let me know. Also, when I ran npm start the first time it started up fine, but now every time I run it I get this error:
code: 'EADDRINUSE',
errno: -48,
syscall: 'listen',
address: '::',
port: 3001
I don't know if this has something to do with it or not.
The first error is caused because your app is not listening to the / path, only the /api path.
The second error is caused by a duplicate app.listen() call in your server/index.js file.
Your code should look something like this in order to resolve the issues:
const express = require("express");
const PORT = process.env.PORT || 3001;
const app = express();
app.get("/", (req, res) => res.send("Main page"));
app.get("/api", (req, res) => {
res.json({ message: "Hello from server!" });
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
I am attempting to use the value for NODE_ENV in my node.js server to connect to a different database when running my tests. How can I get the correct values for NODE_ENV i.e 'development', 'test', 'production' to make what I've got below work?
import pg from "pg";
import dotenv from "dotenv";
dotenv.config();
const Pool = pg.Pool;
const enviroment = () => {
if (process.env.NODE_ENV === "test") {
return process.env.TEST_DATABASE;
} else {
return process.env.DEVELOPMENT_DATABASE;
}
};
const pool = new Pool({
password: process.env.PASSWORD,
host: process.env.HOST,
port: process.env.PORT,
database: enviroment()
});
export default pool;
.env includes
NODE_ENV=development
NODE_ENV=production
NODE_ENV=test
Thanks.
Seems your code is fine. I think you made a typo in your .env file.
Here is a sample for your .env:
NODE_ENV=test
TEST_DATABASE=mongodb://localhost:27017
DEVELOPMENT_DATABASE=mongodb://localhost:27018
PASSWORD=abcxyz
HOST=localhost
PORT=8000
Don't use NODE_ENV="test", spaces, or anything like this.
I am new to node.js and Heroku, I just have deployed for the first time a node.js app to Heroku and when I ran in Heroku, the application did not run so I used the command heroku logs --tail but I had this error:
enter image description here
For idea, my node.js works very fine without Heroku but stop working after deploying it there
Here my index.js:
const express = require("express");
var md5 = require('md5');
var reverseMd5 = require('reverse-md5');
const app = express();
const userRouter = require("./API/users/user.router");
const operationsRouter = require("./API/operations/operations.router");
app.use(express.json());
app.use("", userRouter);
app.use("", operationsRouter);
app.use(express.static('images'));
const port = process.env.APP_PORT || 3000;
app.listen(port, () => {
console.log("server up and running on PORT :", port);
});
Hero my config file java:
const { createPool } = require("mysql");
const pool = createPool({
host: 'host.example.com',
port: 3306,
user: 'user',
password: 'password',
database: 'database',
connectionLimit: 100,
multipleStatements: true
});
module.exports = pool;
What shocked me is that why these errors show up only after deploying on Heroku?
Is there anything I can do to fix this problem?
Heroku dynamically assigns your app a port, so you can't set the port to a fixed number. Heroku adds the port to the env, so you can pull it from there. Switch your APP_PORT listen to this:
.listen(process.env.PORT || 5000)
Make sure you have changed the APP_PORT to PORT.
That way it'll still listen to port 5000 when you test locally, but it will also work on Heroku.
One more thing I can see that you are using Nodemon on Heroku which is not necessary on Heroku. So remove the Nodemon from your Heroku and declare a Procfile
web: node app.js
Being app.js the entry point to your app.
I was trying to deploy my Express + React application to Heroku. Heroku connected successfully with my Github account, then clicking "Deploy Branch" led to "Your app was successfully deployed". But when I went to view my website, it showed:
"Application error An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details".
Here are my logs:
Starting process with command `npm start`
> myproject# start /app
> node backend/index.js
My project SQL server listening on PORT 4000
/app/backend/index.js:22
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
And the index.js which connects to MySQL:
const express = require('express');
const cors = require('cors');
const mysql = require('mysql');
const app = express();
app.use(cors());
app.get('/', (req, res) => {
res.send('go to /my-project to see my project')
});
const pool = mysql.createPool({
connectionLimit: 10,
host: 'localhost',
user: 'root',
password: 'myjs123#',
database: 'my-project',
debug: false
});
pool.getConnection((err, connection) => {
if (err) throw err;
app.get('/my-project', (req, res) => {
connection.query(SELECT_ALL_FACTS_QUERY, (err, results) => {
if (err) {
return res.send(err)
}
else {
return res.json({
data: results
})
};
});
});
});
const SELECT_ALL_FACTS_QUERY = 'SELECT * FROM `my-project`.`my-table`;';
app.listen(4000, () => {
console.log('My project SQL server listening on PORT 4000');
});
What did I do wrong and how could I deploy it?
I think in the below code the localhost should not be used, the localhost will not work in deployment.
const pool = mysql.createPool({
connectionLimit: 10,
//here
host: 'localhost',
user: 'root',
password: 'myjs123#',
database: 'my-project',
debug: false
});
And another mistake I found is you should use an environment variable to store
port numbers. In production, the port number is assigned by Heroku, if not assigned you
can assign. So your code should be
let port=process.env.PORT||4000
app.listen(port, () => {
console.log(`App running on port ${port} `);
});
you need to add (add-ons) to your heroku account
and connect it to your app.
For example, you can use (JAWS_DB mysql)
By having the following code in your connection:
// import the Sequelize constructor from the library
const Sequelize = require('sequelize');
require('dotenv').config();
let sequelize;
// when deployed on Heroku
if (process.env.JAWSDB_URL) {
sequelize = new Sequelize(process.env.JAWSDB_URL);
} else {
// localhost
sequelize = new Sequelize(process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASSWORD, {
host: 'localhost',
dialect: 'mysql',
port: 3306
});
}
module.exports = sequelize;
It passed this stage after I removed if (err) throw err;, still not sure why this happened.
Nithin's answer was taken into account too.
the same errorhappened to me while i was trying to connect to heroku cli and i jus read the heroku config for proxy and that was the case. problem solved by configuring the http and https proxy like
set HTTP_PROXY=http://proxy.server.com:portnumber
or set HTTPS_PROXY=https://proxy.server.com:portnumber