How to Set Up an Express Project
Basic Setup
Create a directory for your project and
cdinto that directory$ mkdir myapp $ cd myappRun
npm initto 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 initInstall Express and save it to the dependencies list
$ npm install --save expressInstall body-parser to handle data passing through your application (e.g. data from forms and/or AJAX requests)
$ npm install --save body-parserCreate a file for your server
$ touch server.jsOpen 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:3000in 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.jsto let your Express app know to look in thepublicdirectory.// 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 thepublicpath when requiring CSS and JavaScript files (since your express app knows to serve files frompublic). It's a good idea to make separate directories withinpublicfor your CSS and JavaScript files (e.g.stylesandscripts).<!-- 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.sendFileto 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.htmlapp.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_modulesdirectory in a.gitignorefile (put this in your project's root directory).- Any file, directory, or path included in
.gitignorewill 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.jsonwill be installed onto their local machine.
- Any file, directory, or path included in