Start node server without as the application starts - node.js

I wish to address an issue that to start the route server, we need to run the command node routes.js to start the server. Is there any way to automatically start it as we start the application.
I am using expo for my react native application.

You can use concurrently installing the NPM (https://www.npmjs.com/package/concurrently) module as follow:
npm install concurrently --save
You can then edit your package.json:
"start": "concurrently \"command1 arg\" \"command2 arg\""
Example:
"start": "concurrently \"npm run server\" \"react-scripts start\""

Related

Running npm concurrently

I have been referencing this post: How can I run multiple npm scripts in parallel?
However, neither of my apps launch with npm run dev. Both apps exit with code 1.
My package.json:
"scripts": {
"start": "ng serve",
"start:lt": "ng serve --configuration=lt",
"dev": "concurrently --kill-others \"npm start\" \"npm start:lt\""
}
And yes, I have concurrently installed.
you can use https://www.npmjs.com/package/foreman module to start app.
first, you need Procfile with commands, you plan to execute in parallel
start: ng serve
start_lt: ng serve --configuration=lt
second, add it to package.json
"scripts": {
"start":"nf start"
}
third - define PORT variable in .env file
PORT=4201
and, finaly, you can start your applicaiton by npm start - it will start foreman executable, that starts both start and start_lt components in parallel

Could not proxy request /user/register from localhost:3000 to http://localhost:3002/

Here back-end project the port is 3002 & front-end port is default 3000 which are different , i added "proxy": "http://localhost:3002" in react project
still getting the proxy issue i don't understand the issue here
please help me thanks in advance
Install concurrently by using npm i concurrently
then add below scripts to your backend pakage.json
This will allow you to run your backend and frontend servers at the same time.
"start": "node server",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run start\" \"npm run client\"",
Then run npm run dev in backend application directory to run the application

npm: missing start script even though start is defined in the package.json file

I have start defined in my scripts on pakcage.json but npm still gives me the start not found error
"scripts": {
"start": "node server.js",
"server":"nodemon server",
"client":"npm start --prefix client",
"dev":"concurrently \"npm run server\" \"npm run client\""
},
This is for Node version 10.16.0 on Windows 10
Basically, you don't need a start script. In your package.json file,
...
"main": "server.js",
...
and in the terminal go to your project directory and type
nodemon
This will start your app upon your configurations!
That should help!

Is it possible to run nodemon to restart server upon changes on front-end?

I'm rather new to this area, so please excuse if this question is completely basic. I've seen some tutorials use nodemon to watch files, and restart servers on Nodejs backend. I've seen others use webpack to watch files such as create react app on frontend. Can you actually use nodemon to watch files and refresh pages on the front end?
Yes I believe you can set it up with your package.json scripts!
For instance with a node server with a create-react-app within a client folder you could declare a start script along the lines of:
"start": "concurrently \"nodemon server.js\" \"cd client && nodemon start\""
Then when you run npm start this will run nodemon on both the server file and the client folder
Just be aware this assumes your server file is named server.js and your client files are in a folder named client and will require you to have the concurrently dependency installed.
You could use something like this one if you already have create react app within a client folder.
"server": "nodemon server.js",
"client": "cd client && yarn start",
"dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\""
Concurrently allows us to run both on one terminal as well as it allows us to pass --kill-others-on-fail which means that if one breaks (control + c on mac), the other one will also break

Attempting to deploy my app (Node, React, Socketio) to Heroku gives me "sh: 1: react-scripts: Permission denied"

So I am trying to make my first react app. I have it working just fine locally. The problem is that I cannot upload my project to Heroku.
This is my app setup:
my file arrangement. I built this starting with a create-react-app, wrapped that entire thing in a "client" folder, and created a normal node server arrangement outside. I haven't touched my react app's package.json" except to add a socketio dependency, other than that it is exactly what create-react-app spits out.
I get this error on console when attempting to upload:
"sh: 1: react-scripts: Permission denied"
I have gone through https://devcenter.heroku.com/articles/nodejs-support, but to no avail.
This is my "outside" package.json scripts and dependencies
"scripts": {
"client": "npm start --prefix client",
"server": "node server.js",
"start": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "cd client && npm run build"
},
"dependencies": {
"concurrently": "^4.0.1",
"express": "^4.16.4"
"socket.io": "^2.1.1"
}
UPDATE:
I've not gotten my application to work yet HOWEVER I finally found something interesting.
This: https://github.com/mars/heroku-cra-node
Looks like a barebones application I can essentially my stuff to, and have it work on Heroku. I've not added my code yet, I'll report back.
UPDATE:
Yep, seems to get past this permission BS. Now I just have to deal with getting socket.io client to connect... -_-
Change your script to this:
"scripts": {
"start": "node index.js",
"server": "nodemon index.js",
"client": "npm run start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_PRODUCTION=false npm install --prefix client && npm run build --prefix client",
},
npm run dev will start your server and client in development mode...
You can also install nodemon to make your life easier...

Resources