How to Set Up an Express Project
Basic Setup
Create a directory for your project and
cd
into that directory$ mkdir myapp $ cd myapp
Run
npm init
to create yourpackage.json
, which will keep track of your dependencies. Press enter to go through all the options it presents (you many want to change entry point (filename of your server) or version number, but it's also fine not to change anything, especially if you're making a test app).$ npm init
Install Express and save it to the dependencies list
$ npm install --save express
Install body-parser to handle data passing through your application (e.g. data from forms and/or AJAX requests)
$ npm install --save body-parser
Create a file for your server
$ touch server.js
Open your project in Sublime, and set up your server in
server.js
// require express framework and additional modules var express = require('express'), app = express(), bodyParser = require('body-parser'); // tell app to use bodyParser middleware app.use(bodyParser.urlencoded({extended: true})); // set up root route to respond with 'hello world' app.get('/', function (req, res) { res.send('hello world'); }); // listen on port 3000 app.listen(3000, function () { console.log('server started on localhost:3000'); });
Back in the terminal, in your project's root directory, run either
node server.js
,npm start
, ornodemon
(docs). All three commands do the same thing, which is start your server :)Visit
localhost:3000
in the browser. Make sure you see your "hello world" response before moving further.
Serving Static Assets
You need to tell your Express app where to look for your CSS files and client-side JavaScript. Add the following line to your
server.js
to let your Express app know to look in thepublic
directory.// serve js and css files from public folder app.use(express.static(__dirname + '/public'));
In your view (
index.html
), you don't need to specify thepublic
path when requiring CSS and JavaScript files (since your express app knows to serve files frompublic
). It's a good idea to make separate directories withinpublic
for your CSS and JavaScript files (e.g.styles
andscripts
).<!-- stylesheet --> <link type="text/css" rel="stylesheet" href="styles/main.css"> <!-- client-side javascript --> <script type="text/javascript" src="scripts/main.js"></script>
Serving HTML Files
We can use
res.sendFile
to tell our Express app to server static HTML files. Similar to serving static assets, we have to let our Express app know where to look, in this case/public/views/index.html
app.get('/', function (req, res) { res.sendFile(__dirname + '/public/views/index.html'); });
More Tips
- If you're putting your project on GitHub, it's best-practice to include your
node_modules
directory in a.gitignore
file (put this in your project's root directory).- Any file, directory, or path included in
.gitignore
will remain untracked by GitHub. Installing node modules generates a lot of extra files, and it's nice to keep them out of your GitHub repo to save space. - Any developer who wants to clone your project on GitHub and run it locally can run
npm install
, and all the node modules listed in yourpackage.json
will be installed onto their local machine.
- Any file, directory, or path included in