I don't understand the difference of deploying a site "statically" vs. using express. The docs in create-react-app says you don't need express to spin up a server? So where and how do you deploy this app? I don't get it. I'm used to creating a server.js
with express and then deploying to something like Heroku in the past.
Deployment
npm run build creates a build directory with a production build of your app. Set up your favourite HTTP server so that a visitor to your site is served index.html, and requests to static paths like /static/js/main
.<hash>
.js are served with the contents of the /static/js/main.<hash>.js
file.
From their docs:
Static Server
For environments using Node, the easiest way to handle this would be to install serve and let it handle the rest:
npm install -g serve
serve -s build
The last command shown above will serve your static site on the port 5000
. Like many of serve’s internal settings, the port can be adjusted using the -p
or --port
flags.
Run this command to get a full list of the options available:
serve -h
Other Solutions
You don’t necessarily need a static server in order to run a Create React App project in production. It works just as fine integrated into an existing dynamic one.
Here’s a programmatic example using Node and Express:
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(9000);
I don't understand why you wouldn't want to run it with express (what I guess is called "dynamically"?). How would that work if you don't have express setup anyway? I don't get "running this statically". I've only ever run apps with express in the past.
via PositiveGuy
No comments:
Post a Comment