How to build my express backend for production? - node.js

I wrote an Express Server. How can I build it now to deploy it on a WebApp? I don't have any build scripts in the project..

Don't you have any script in your package.json ? Should be something like "start": "node ."

Related

Deploying React + Express on Azure

I'm having that problem where I'm trying to deploy Express and React boilerplate app like one app on Azure App Services. Locally everything works normaly, but when I try to deploy the whole thing, I see ":( Application Error " screen. I think it has to do something with the way my "scripts" inside server's package.json are set...(deploying only express server without client files works just fine). Here I'm trying to let Azure execute the build command once the app is deployed and not before that:
"scripts": {
"start": "cd client && npm run build && cd ../ && node server",
"test": "echo something"
},
Probably I'm missing something crucial for Azure (I'm a beginner).I've read a lot and followed many tutorials but nothing seems to solve my problem. I'll really appreciate it if someone can give me a hand. Thank you!
Link to my github repo : https://github.com/Ivailo663/finalExpressApp
"scripts": { "start": "cd client && npm run build && cd ../ && node server", "test": "echo something" }
This script will work if you already have all the modules your app needed, but if you don't have the modules, you need to install them first.
However, it is recommended that you build the client locally or use pipline to deploy the build folder to azure.

Configure ports in WebStorm - React + Node Express app

I am going to do a fullstack implementation with WebStorm IDE. I use react.js and node-express for this. I can do this in separate projects. But I want to know how to implement both backend and frontend in WebStorm project while configuring ports.
When starting the frontend we can give start html file in package.json file under "start": "parcel index.html". When starting backend we can give "node app.js" But this can be done when we are doing the implementation in two different projects.
But if we are doing both frontend and backend how can I start react part and node-express part on two different ports? For example:
- react app > localhost:1234
- node-express > localhost:3000
"start": "parcel index.html"
"start": "node app.js"
// in package.json we can set only one start
You can use a package like npm-run-all to achieve this. Install it then have two separate scripts (like start-front-end and start-server) in your package.json that npm-run-all runs both of e.g.
"scripts": {
"start-front-end": "parcel index.html --open",
"start-server": "node app.js",
"start": "npm-run-all start-server start-front-end",
}

How to deploy an npm project to Heroku?

I currently have an npm project built with vue-cli and socket.io server.
This is how my project is structured:
Project/
|--node_server/
| |--server.js
|--src/
| |--main.js
| |--App.vue
| |--other .vue files and folders
I do not know how to deploy this app on Heroku because I will need to run two scripts while deploying that is node server.js (in the node_server folder) and npm run build or npm run dev (in the root folder).
What are the steps on how to deploy it successfully? Heroku takes my project from github whenever I push and builds it automatically. I have tried deploying but it ends up with an error page.
Let's say you need to build the front-end with build script first, and then you need to run nodejs server with start script which is node server.js.
...
"scripts": {
"build": "gulp or something idk"
"prestart": "npm run build",
"start": "node node_server/server.js",
},
...
But if you need to run these two scripts at the same time you can achieve this with something like that:
...
"scripts": {
"build": "gulp or something idk"
"start": "npm run build & node node_server/server.js",
},
...
I hope it would be helpful.
For an example, you can take a look at the package.json of one of my project: https://github.com/hisener/pirate-radio/blob/master/package.json
For more information, please refer https://docs.npmjs.com/misc/scripts

How to run 2 different commands on deploy and restart using Node.js on Heroku?

I am writing a Node.js app and using Heroku to host it. And also I use Webpack + Babel to bundle all my server files into one and to use ES2015 syntax. But the thing is, I need to build my app before running it. So I put this command into package.json:
"scripts": {
"start": "webpack && node build/server.js"
},
This works, but the problem is, when I run heroku restart, my app runs only after rebuild. And the same story when my app crashes.
So I guess I need 2 different commands: one on deploy (webpack) and one in npm start (node build/server.js)
How can I accomplish this?
Actually it was not that hard. Just had to change my package.json to
"scripts": {
"heroku-postbuild": "webpack",
"start": "node build/server.js"
}
(according to this article: Heroku Node.js Support)

Running a Grunt build task on Heroku

I am deploying a Node.js application on Heroku. My production deployment is based on a Grunt task that minifies js and css files, placing them in a /dist folder from which they are called.
How can I add this step on the Heroku deployment?
Use a postinstall script, as outlined in Heroku's DevCenter docs for node:
https://devcenter.heroku.com/articles/node-with-grunt
eg:
"scripts": {
"postinstall": "grunt uglify",
"start": "node server.js"
}

Resources