I can't figure out how to get my react app (created with create-react-app) to work when deploying to heroku.
Without heroku everything works fine. I use npm start which uses react-scripts start to start the react app and everything works locally as expected.
Now that I want the app to run on heroku I have to use express to serve it's content to the client. I tried this but sadly my browser stays blank. Just a white screen, nothing else. Not even error messages?! Neither in the console nor in the browser's console.
My server's structure looks like this:
public
favicon.ico
index.html
manifest.json
etc...
src
App.css
App.js
index.css
index.js
etc...
package-lock.json
package.json
Procfile
server.js
Here is my server.js file:
const path = require('path');
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'src')));
app.get('/', (req, res) => {
res.sendFile(path.join(publicPath, 'index.html'));
});
app.listen(port, () => {
console.log('Server is running on port ' + port);
});
Is there any mistake in the file? Or do I maybe use a wrong method to start the server?
I've seen people using node server.js, some run node server.js in one terminal and npm start in another terminal. And some others run just npm start but changed the start script in the package.json to node server.js. I think I am confused at this point. What's the difference between all those commands?
And what could I do to solve the problem? Can anyone help me? Or do you need any additional code insights to help me?
My server.js
const express = require('express');
const http = require('http');
const path = require('path');
let app = express();
app.use(express.static(path.join(__dirname, 'build')));
const port = process.env.PORT || '8080';
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log(`Running on :${port}`));
My Procfile as per heroku:
web: node server.js
Please note that you should have build folder where the production would be picked up by heroku. In addition package.json.
My package.json
{
"name": "my-app",
"version": "3.0.1",
"main": "index.js",
"license": "MIT",
"dependencies": {
"#amcharts/amcharts4": "^4.0.3",
"#amcharts/amcharts4-geodata": "^4.0.27",
"#babel/runtime": "7.0.0-beta.55",
"#date-io/moment": "^1.1.0",
"#material-ui/core": "^3.8.1",
"#material-ui/icons": "^3.0.1",
"array-move": "^1.0.0",
"autosuggest-highlight": "^3.1.1",
"axios": "^0.18.0",
"babel-preset-stage-2": "^6.24.1",
"bootstrap": "^4.1.3",
"bootstrap-v4-rtl": "^4.1.1-0",
"can-use-dom": "^0.1.0",
"chart.js": "^2.7.3",
"classnames": "^2.2.6",
"connected-react-router": "^6.2.1",
"cross-env": "^5.2.0",
"downshift": "^3.1.5",
"draft-js": "^0.10.5",
"draftjs-to-html": "^0.8.4",
"email-regex": "^3.0.0",
"express": "^4.17.1",
"history": "^4.7.2",
"install": "^0.10.1",
"isomorphic-fetch": "^2.2.1",
"jss-rtl": "^0.2.3",
"jwt-decode": "^2.2.0",
"luhn": "^2.3.0",
"material-ui-pickers": "^2.0.1",
"moment": "^2.22.2",
"node-sass": "^4.10.0",
"node_env": "^0.0.3",
"nprogress": "^0.2.0",
"package.json": "^2.0.1",
"path": "^0.12.7",
"prop-types": "^15.6.2",
"query-string": "^6.4.0",
"react": "^16.6.3",
"react-autosuggest": "^9.4.2",
"react-big-calendar": "^0.20.2",
"react-bootstrap-sweetalert": "^4.4.1",
"react-chartjs-2": "^2.7.4",
"react-circular-progressbar": "^1.0.0",
"react-ckeditor-component": "^1.1.0",
"react-color": "^2.17.0",
"react-custom-scrollbars": "^4.2.1",
"react-d3-speedometer": "^0.4.2",
"react-device-detect": "^1.6.1",
"react-dnd": "^6.0.0",
"react-dnd-html5-backend": "^6.0.0",
"react-dom": "^16.6.3",
"react-draft-wysiwyg": "^1.12.13",
"react-dropzone": "^4.2.9",
"react-google-maps": "^9.4.5",
"react-hot-loader": "^4.3.12",
"react-icons": "^3.2.2",
"react-intl": "^2.7.2",
"react-joyride": "^1.11.4",
"react-jss": "^8.6.1",
"react-jvectormap": "^0.0.4",
"react-loadable": "^5.5.0",
"react-notifications": "^1.4.3",
"react-number-format": "^4.0.4",
"react-paypal-express-checkout": "^1.0.5",
"react-placeholder": "^3.0.1",
"react-redux": "^6.0.0",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-select": "^2.1.1",
"react-simple-maps": "^0.12.1",
"react-slick": "^0.23.1",
"react-sortable-hoc": "^0.8.3",
"react-star-rating-component": "^1.4.1",
"react-swipeable-views": "^0.13.0",
"react-text-mask": "^5.4.3",
"reactstrap": "^6.5.0",
"recharts": "^1.3.6",
"recompose": "^0.30.0",
"redux": "^4.0.1",
"redux-logger": "^3.0.6",
"redux-persist": "^5.10.0",
"redux-saga": "^0.16.2",
"save": "^2.3.2",
"slick-carousel": "^1.8.1",
"typeface-roboto": "^0.0.54",
"underscore": "^1.9.1",
"url-search-params": "^1.1.0"
},
"scripts": {
"start": "cross-env NODE_PATH=src react-scripts start",
"build": "cross-env NODE_PATH=src react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"devDependencies": {
"bootstrap": "^4.1.3",
"node-sass": "^4.9.3",
"prettier": "^1.14.2",
"react-scripts": "2.1.1"
},
"browserslist": {
"development": [
"last 2 chrome versions",
"last 2 firefox versions",
"last 2 edge versions"
],
"production": [
">0.25%",
"not op_mini all",
"ie 11"
]
},
"engines": {
"node": ">=6.9.0",
"npm": ">= 3"
}
}
Heroku will run npm run build by default when deploying. In your case this is react-scripts build. This script will bundle your code into a build folder.
Express
This means that your Express middleware should serve static files from your build folder instead of your src folder.
app.use(express.static(path.join(__dirname, 'src')));
should be:
app.use(express.static(path.join(__dirname, 'build')));
(Note: I suggest using an environment variable for this process.env.STATIC_DIR or something similar. This way you can easily switch between src for local development and build for production code and deployment.)
Procfile
Your procfile states which command Heroku should run after deploying your code. Knowing that Heroku runs npm run build you should serve your bundle instead of your source code.
You can do this by changing your procfile like this:
web:npm start
Should be:
web:node server.js
This will run your server.js file which serves your static content from the build folder.
(Note: npm run start is just for local development. When deploying your web app you should only deploy production code.)
(Note: You can test your production build locally by running npm run build to bundle your code and node server.js to serve it.)
Yeah, create-react-app doesn't play nicely with Node.
This is what I did to fix my deployment.
tl;dr :
Since you created the react app with create-react-app you need to use a different buildpack such as: github.com/mars/create-react-app-buildpack.git
An example workflow taken from Heroku's docs:
# You probably did this already
# Create the App
npm install -g create-react-app
create-react-app my-app
cd my-app
git init
# Then you worked on your app
# Now, create the app in heroku with this buildpack...
heroku create -b https://github.com/mars/create-react-app-buildpack.git
# ...or add the buildpack in Heroku's dashboard
# Add the files the buildpack bundled for you and deploy
git add .
git commit -m "react-create-app on Heroku"
git push heroku master
# To see your app
heroku open
The Procfile (in the root directory) should contain the following:
web: bin/boot
Once that's in heroku's repo, you can use the CLI to start the web dyno
heroku ps:scale web=1:free
You can delete your server.js file at this point:
More info here: https://github.com/mars/create-react-app-buildpack#usage
Hope this helps. Cheers!
Related
When I work on my project locally (without problem), I do npm start which works on port 3000, also i have to open another terminal and do node server/server.js which works on port 4000. Then I will be able to work with my browser to connect my both frontend and backend. Now I am trying to host this project on heroku but no luck. here are my error:
2020-08-26T11:54:23.905587+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=whatever.herokuapp.com request_id="whatever" fwd="96.20.56.73" dyno=web.1 connect=1ms service=159ms status=503 bytes=0 protocol=https
and package.json:
{
"name": "library",
"version": "0.1.0",
"private": true,
"proxy": "http://localhost:4000",
"dependencies": {
"#material-ui/core": "^4.9.9",
"#material-ui/icons": "^4.9.1",
"#material-ui/lab": "^4.0.0-alpha.48",
"#material/react-snackbar": "^0.15.0",
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.0",
"#testing-library/user-event": "^7.2.1",
"chai": "^4.2.0",
"clsx": "^1.1.0",
"cookie-parser": "^1.4.4",
"dotenv": "^8.2.0",
"google-maps-react": "^2.0.2",
"hookrouter": "^1.2.3",
"i18next": "^19.4.1",
"i18next-browser-languagedetector": "^4.0.2",
"i18next-xhr-backend": "^3.2.2",
"immer": "^5.3.6",
"mongo-seeding": "^3.4.1",
"mongodb": "^3.5.3",
"multer": "^1.4.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-facebook-login": "^4.1.1",
"react-google-login": "^5.1.1",
"react-hook-google-maps": "0.0.3",
"react-i18next": "^11.3.4",
"react-redux": "^7.1.3",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"redux": "^4.0.5",
"redux-immer": "^1.0.4",
"sha1": "^1.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "mocha --exit",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^3.0.0",
"eslint-plugin-react-redux": "^3.0.3",
"mocha": "^7.2.0",
"supertest": "^4.0.2"
}
}```
is it cause of the proxy? or i have a wrong start script? or something else?
The way I deploy on heroku is:
to the backend package.json scripts I add
"heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
add the following to my server.js
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (request, response) => {
response.sendFile(path.join(__dirname, "client/build", "index.html"));
});
}
After these I just git push heroku master and it gets deployed w/o any issues.
I was using create-react-app + JSX, so i had to open two terminals one for npm start (on port 3000 for react server) and one for node server.js (for backend on port 4000) and these two are connect through the proxy that i had on my package.json. when i have finished with development, all i had to do was:
remove the proxy from package.json.
npm run build, which makes a copy of all my code into the build folder.
app.use("/", express.static("build"));
in my server.js file to make the build folder accessible to app.
app.all("/*", (req, res) => { res.sendFile(__dirname + "/build/index.html"); }); at the end of the server.js to catch all.
hope this saves some of your time.
I am trying to deploy my angular app to heroku, the builds are successful but when i go to my link, it is telling me 'Not Found'. I am really confused as to what is going on because I have visited various websites which go through the steps and I am sure that I am following the steps properly.
I have a procfile which has
web: node server.js
My package.json looks like this -
{
"name": "ng-barry-project",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "node server.js",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"heroku-postbuild": "ng build --aot --prod"
},
"private": true,
"dependencies": {
"#angular/animations": "~8.2.0",
"#angular/cli": "^8.3.17",
"#angular/common": "~8.2.0",
"#angular/compiler": "~8.2.0",
"#angular/compiler-cli": "^8.2.13",
"#angular/core": "~8.2.0",
"#angular/forms": "~8.2.0",
"#angular/platform-browser": "~8.2.0",
"#angular/platform-browser-dynamic": "~8.2.0",
"#angular/router": "~8.2.0",
"#fortawesome/angular-fontawesome": "^0.5.0",
"#fortawesome/fontawesome-svg-core": "^1.2.25",
"#fortawesome/free-solid-svg-icons": "^5.11.2",
"express": "^4.17.1",
"path": "^0.12.7",
"rxjs": "~6.4.0",
"tslib": "^1.10.0",
"typescript": "~3.5.3",
"zone.js": "~0.9.1"
},
"devDependencies": {
"#angular-devkit/build-angular": "~0.802.2",
"#angular/cli": "^8.3.17",
"#angular/compiler-cli": "^8.2.13",
"#angular/language-service": "~8.2.0",
"#types/jasmine": "~3.3.8",
"#types/jasminewd2": "~2.0.3",
"#types/node": "~8.9.4",
"codelyzer": "^5.0.0",
"enhanced-resolve": "^3.3.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.5.3"
},
"engines": {
"node": "12.9.1",
"npm": "6.10.2"
}
}
This is my server.js file
//Install express server
const express = require('express');
const path = require('path');
const app = express();
// Serve only the static files form the dist directory
app.use(express.static('./dist/pi-project'));
app.get('/*', function(req,res) {
res.sendFile(path.join(__dirname,'/dist/pi-project/index.html'));
});
// Start the app by listening on the default Heroku port
app.listen(process.env.PORT || 8080);
Please try like this .click here
Everything seems correct, just cross check the following points if it may help
You might get error running the postinstall command. This works instead:
heroku-postbuild": "ng build --prod
Node and npm engine should be same version you have on your machine. So, run node -v and npm -v to get the correct version and include it in your package.json
This worked for me for angular 6 and angular 7 deployment on heroku.
Try this- https://www.youtube.com/watch?v=hE1i_LhL_Nc
Updated Answer
You need a couple of Edits to your server.js file
Change this:
app.use(express.static('./dist/pi-project'));
To This:
app.use(express.static(path.join(__dirname,'./dist/pi-project')));
And Change this:
app.get('/*', function(req,res) {
To this
app.get('/', function(req,res) {
so that not all requests are serviced by this function
Original Answer
In your server.js you have the following:
app.get('/*', function(req,res) {
res.sendFile(path.join(__dirname,'/dist/pi-project/index.html'));
});
Because of the asterisk after the /, that's saying to return the index.html file for every request that comes in. So what will happen is that you'll first make a request to localhost:8080 and that will return the index.html (so far so good). Then the browser will read the <script> tags from the index.html and for each one it will make another request. So let's say the next request will be to localhost:8080/main.js. When express sees that it will AGAIN return index.html which obviously isn't what you want.
To fix that problem you can change your server.js to this:
//Install express server
const express = require('express');
const path = require('path');
const app = express();
// Serve only the static files form the dist directory
app.use(express.static('./dist/pi-project'));
app.get('/*', function(req,res) {
if(req.url === '/') {
res.sendFile(path.join(__dirname,'/dist/pi-project/index.html'));
}
else {
res.sendFile(path.join(__dirname, '/dist/pi-project' + req.url))
}
});
// Start the app by listening on the default Heroku port
app.listen(process.env.PORT || 8080);
I would like to do a Hot-Deployment of the Front-End-Code (HTML, CSS, JS) of some Portlet so development time can be saved and not the whole Portlet needs to be redeployed.
The Portlet is a .war File.
In the good old Liferay 6.2 this was simply possible by overwriting the static frontend Code in the tomcat/webapps/portlet-name directory.
In Liferay DXP this is no longer possible, since the Portlet is not extracted to tomcat/webapps/ anymore.
Is there any possibility for a frontend-Hot-Deploy, so i can change e.g. my .html file on the fly and I don't have to redeploy the whole .war Portlet?
Apart from the described method being really bad practice for maintenance, I'm wondering how much time you actually save (as you say that this is your motivation).
You can always implement your portlet to include various resources from places outside of Liferay's control, but naturally, the provided tools won't help you keeping everything in sync - that'll be your personal problem to solve.
By the way: Tomcat's mode, in which you are able to just replace random content in the webapps directory, is called development mode.
We found an alternative solution to improve our time for frontend development, but it works completly different as it did in the old Liferay 6.2.
Basically we use an express.js proxy Server, which runs in parallel to Liferay on some different Port. This Proxy Server forwards all the requests to the other running liferay, except the requests for HTML, CSS and JS Files. Those are directly served from the local file System instead. Also an automatic rebuild of the frontend is triggered when a HTML, CSS or JS File is changed and saved.
This small Proxy-Server consists basically out of those two Files:
The dev-server.js File:
const packageConfig = require('../../../package.json');
const projectName = packageConfig.name;
const config = Object.assign({}, {
port: 8088,
liferayUrl: 'http://localhost:8080',
liferayVersion: 7,
webpackConfigFile: './webpack.development.config.js',
}, packageConfig.devServer || {});
const path = require('path');
const webpack = require('webpack');
const middleware = require('webpack-dev-middleware');
const webpackCompiler = webpack(require(config.webpackConfigFile));
const express = require('express');
const app = express();
const httpProxy = require('http-proxy');
const liferayProxy = httpProxy.createProxyServer();
let publicPath = `/o/${projectName}/static/js/`;
if(config.liferayVersion === 6) {
publicPath = `/${projectName}/static/js/`
}
app.use(
middleware(webpackCompiler, {
publicPath: `/o/${projectName}/static/js/`
})
);
app.all('/*', function(req, res) {
liferayProxy.web(req, res, {target: config.liferayUrl});
});
app.listen(config.port, () => console.log('Development server listening on port ' + config.port + '!'));
And the package.json:
{
"name": "react-base",
"version": "1.0.0",
"license": "NOLICENSE",
"private": true,
"scripts": {
"preinstall": "node ./target/ui-build/build/preInstallHook.js",
"build-dev": "gulp --gulpfile ./target/ui-build/build/gulpfile.js && webpack --config ./target/ui-build/build/webpack.development.config.js --progress --profile",
"build": "gulp --gulpfile ./target/ui-build/build/gulpfile.js production && webpack --config ./target/ui-build/build/webpack.production.config.js --bail",
"lint": "eslint -c ./target/ui-build/build/.eslintrc.js --rulesdir \"node_modules/#myproject/react-component-lib/eslint-rules/\" \"src/main/react/**/*.js\" ",
"test": "jest --config=./target/ui-build/build/jest.config.js --rootDir=./ --passWithNoTests",
"coverage": "jest --config=./target/ui-build/build/jest.config.js --rootDir=./ --passWithNoTests --coverage",
"stats": "webpack-bundle-analyzer ./target/generated-sources/js/stats.json",
"start:dev": "node ./target/ui-build/build/dev-server.js"
},
"dependencies": {
"#babel/runtime": "^7.0.0",
"mobx": "3.1.16",
"mobx-react": "4.4.3",
"prop-types": "15.7.2",
"react": "16.8.4",
"react-dom": "16.8.4"
},
"devDependencies": {
"autoprefixer": "^9.1.5",
"babel-core": "^7.0.0-bridge.0",
"#babel/core": "7.1.0",
"babel-eslint": "10.0.1",
"babel-jest": "^23.0.0",
"babel-loader": "8.0.4",
"#babel/plugin-proposal-class-properties": "7.1.0",
"#babel/plugin-proposal-decorators": "7.1.0",
"#babel/plugin-proposal-object-rest-spread": "7.0.0",
"#babel/plugin-transform-runtime": "7.1.0",
"#babel/preset-env": "7.1.0",
"#babel/preset-react": "7.0.0",
"css-loader": "1.0.0",
"enzyme": "3.4.0",
"enzyme-adapter-react-16": "1.5.0",
"eslint": "4.19.1",
"eslint-plugin-jsx-a11y": "6.0.3",
"eslint-plugin-react": "7.11.1",
"express": "4.17.1",
"file-loader": "2.0.0",
"fs-extra": "7.0.0",
"gulp": "3.9.1",
"gulp-concat": "2.6.1",
"http-proxy": "1.17.0",
"identity-obj-proxy": "3.0.0",
"jest": "^23.0.0",
"jest-cli": "^23.0.0",
"node-sass": "4.9.3",
"postcss-loader": "3.0.0",
"raf": "3.4.1",
"react-test-renderer": "16.8.4",
"run-sequence": "1.2.2",
"sass-loader": "7.1.0",
"style-loader": "0.23.1",
"url-loader": "1.1.2",
"url-search-params-polyfill": "5.0.0",
"webpack": "4.20.2",
"webpack-bundle-analyzer": "^3.0.2",
"webpack-cli": "^3.1.1",
"webpack-dev-middleware": "3.7.0"
},
"sideEffects": [
"*.css",
"*.scss"
]
}
The Proxy-Server can be started by calling 'yarn run start:dev', then you can access Liferay over the Proxy-Server via http://localhost:8088
Hi is there anyone who has tried to deploy nuxt app to Google App Engine. I have tried from nuxt regular and express template, it shows 502 Bad Gateway. I don't modify anything from create-nuxt-app command. My app.yaml file contains
runtime: nodejs
env: flex
Is there anything wrong with my setup or maybe there are some additional setup I have to do?
Here is my package.json
{
"name": "nuxt-pwa-vuetify-starter",
"version": "1.0.0",
"description": "Nuxt.js + PWA + Vuetify.js starter project",
"author": "Jefry Dewangga <jefrydco#gmail.com>",
"private": true,
"homepage": "https://github.com/jefrydco/nuxt-pwa-vuetify-starter#readme",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/jefrydco/nuxt-pwa-vuetify-starter"
},
"keywords": [
"nuxt",
"nuxt.js",
"nuxtjs",
"nuxt pwa",
"vue",
"vue.js",
"vuejs",
"vue universal",
"vue ssr",
"vue pwa",
"vuetify",
"vuetify.js",
"vuetifyjs"
],
"engines": {
"node": ">=8.0.0",
"npm": ">=5.0.0"
},
"scripts": {
"dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server",
"build": "nuxt build",
"prestart": "npm run build",
"start": "cross-env NODE_ENV=production node server/index.js",
"generate": "nuxt generate"
},
"dependencies": {
"#nuxtjs/axios": "^5.0.1",
"#nuxtjs/browserconfig": "0.0.7",
"#nuxtjs/component-cache": "^1.1.1",
"#nuxtjs/dotenv": "^1.1.0",
"#nuxtjs/google-analytics": "^2.0.2",
"#nuxtjs/pwa": "^2.0.5",
"#nuxtjs/sentry": "^1.0.1",
"#nuxtjs/sitemap": "0.0.3",
"babel-plugin-transform-imports": "^1.4.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-stage-0": "^6.24.1",
"babel-runtime": "^6.26.0",
"cross-env": "^5.1.3",
"express": "^4.16.2",
"morgan": "^1.9.0",
"node-sass": "^4.7.2",
"nodemon": "^1.17.1",
"nuxt": "^1.3.0",
"pug": "^2.0.0-rc.4",
"sass-loader": "^6.0.7",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
"vuetify": "^1.0.3"
},
"devDependencies": {
"babel-eslint": "^8.2.2",
"eslint": "^4.18.1",
"eslint-config-standard": "^10.2.1",
"eslint-loader": "^1.9.0",
"eslint-plugin-html": "^4.0.2",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1"
}
}
and here is my app server index.js,
const express = require('express')
const { Nuxt, Builder } = require('nuxt')
const app = express()
const host = process.env.HOST || '127.0.0.1'
const port = process.env.PORT || 8080
app.set('port', port)
// Import and Set Nuxt.js options
let config = require('../nuxt.config.js')
config.dev = !(process.env.NODE_ENV === 'production')
async function start () {
// Init Nuxt.js
const nuxt = new Nuxt(config)
// Build only in dev mode
if (config.dev) {
const builder = new Builder(nuxt)
await builder.build()
}
// Give nuxt middleware to express
app.use(nuxt.render)
// Listen the server
app.listen(port, host)
console.log('Server listening on http://' + host + ':' + port) // eslint-disable-line no-console
}
start()
Below worked for me.
package.json
"start": "npm install --save cross-env && nuxt build && cross-env NODE_ENV=production node server/index.js",
This install cross-env before serving and nuxt build is a required command in production.
Plus I've changes in server.js
Add health route to express:
app.get('/_ah/health', (req, res) => {
res.status(200)
res.send({hello:'world'})
})
Listen to only port
// app.listen(host,port)
app.listen(port)
I uninstalled webpack and removed the webpack.config.js file so that I could use express.js with Heroku. After committing and pushing to heroku, my log reads like this:
$ heroku logs
2017-09-10T17:42:43.224045+00:00 heroku[web.1]: State changed from crashed to starting
2017-09-10T17:42:02.000000+00:00 app[api]: Build succeeded
2017-09-10T17:42:47.480825+00:00 heroku[web.1]: Starting process with command `node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js`
2017-09-10T17:42:50.050087+00:00 app[web.1]: module.js:471
2017-09-10T17:42:50.050108+00:00 app[web.1]: throw err;
2017-09-10T17:42:50.050108+00:00 app[web.1]: ^
2017-09-10T17:42:50.050109+00:00 app[web.1]:
2017-09-10T17:42:50.050110+00:00 app[web.1]: Error: Cannot find module '/app/node_modules/webpack-dev-server/bin/webpack-dev-server.js'
The starting process in the log is trying to initiate webpack ^, but in my Procfile I have this:
web: node server.js
To confirm that, here is heroku ps
$ heroku ps
=== web (Free): node server.js (1)
web.1: up 2017/09/10 14:41:56 -0400 (~ 27m ago)
package.json:
{
"name": "youtube-instant-search",
"version": "1.0.0",
"description": "Renders youtube videos after keystrokes",
"main": "index.js",
"repository": "git#github.com:mattwarren10/youtube-instant-
search.git",
"scripts": {
"start": "node server.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel": "^5.8.29",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^6.4.1",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2016": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"babelify": "^7.3.0",
"bootstrap": "^4.0.0-alpha.6",
"browserify": "^14.4.0",
"css-loader": "^0.28.7",
"eslint": "^4.6.1",
"node-sass": "^4.5.3",
"rimraf": "^2.6.1",
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2"
},
"dependencies": {
"#types/react-addons-create-fragment": "^0.14.17",
"babel-preset-stage-1": "^6.1.18",
"core-js": "^2.5.1",
"express": "^4.15.4",
"lodash": "^3.10.1",
"prop-types": "^15.5.10",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"react-intl": "^2.3.0",
"react-redux": "^4.0.0",
"redux": "^3.0.4",
"youtube-api-search": "0.0.5"
}
}
server.js:
var express = require('express');
var app = express();
var path = require('path');
app.use(express.static(path.join(__dirname)));
app.use("/style", express.static(__dirname));
// viewed at based directory http://localhost:8080/
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + 'index.html'));
});
app.listen(process.env.PORT || 8080);
Does it take time for heroku to update? What's going on?