react-starter project
suggest changeAbout this Project
This is a simple boilerplate project. This post guides you through setting up ReactJS with an Express static server and in-browser Babel. Despite the introduction, the steps below do not install Webpack.
Let's get started
You need the node package manager to start an Express server and manage dependencies. If you are new to npm, you can check here. Installing npm is required.
Create a folder and open it in a terminal. Run npm init. That creates package.json and asks for the project name, version, description, entry point, git repository, author, license, and so on. The entry point matters because Node looks for it when you run the project. Confirm the answers at the end. That finishes package.json.
Express server setup. Run npm install express@4 --save. The --save flag matters: without it package.json is not updated. package.json stores the dependency list. This adds Express 4. Your package.json will look like:
"dependencies": {
"express": "^4.13.4",
.............
},
After the install finishes you will see a node_modules folder. In the project root, create server.js. The Express server is below; the explanation follows.
var express = require('express');
// Create our app
var app = express();
app.use(express.static('public'));
app.listen(3000, function () {
console.log('Express server is using port:3000');
});
var express = require('express'); loads the Express API.
var app = express(); calls that function. app.use() adds functionality to the application. app.use(express.static('public')); exposes the public folder. app.listen(3000, function () {}) listens on port 3000 and logs that the server is running. That sets up Express.
Create a public folder and an index.html file inside it. Express serves index.html by default from that folder. A simple file looks like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<h1>hello World</h1>
</body>
</html>
From the project directory, run node server.js. The terminal should print Express server is using port:3000.
Open http://localhost:3000. You will see hello World.
Inside public, create app.jsx. JSX is a syntax extension that adds XML-like syntax to JavaScript. You can use React without JSX, but JSX is easier to read. Sample app.jsx:
ReactDOM.render(
<h1>Hello World!!!</h1>,
document.getElementById('app')
);
Update index.html so it looks like this. The script URLs must not contain spaces:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel" src="app.jsx"></script>
</body>
</html>
With this in place you are all done, I hope you find it simple.