Express.js Tutorial
Looking for a good tutorial on Express.js to help you get quickly productive in it? You have come to the right place.
In this tutorial I will run you through the process setting up an Express.js app and making it do what a basic website might do. You will learn the basics of routes, views, Jade templates, Stylus CSS engine, handling POST and GET requests.
Let's create an online store to sell Ninja items. We will call it the Ninja Store.
Installing Express
First of all install Express.js as a global module:
$ npm install express -g
If that fails:
$ sudo npm install express -g
Now, create a directory and create an Express app in it:
$ mkdir ninja-store
$ cd ninja-store
$ express --sessions --css stylus
If you like shortcuts, you could accomplish the same this way too:
$ express ninja-store --sessions --css stylus && cd ninja-store
We want to have support for sessions, hence the --sessions option. Using --css, we specified that we would be using the Stylus CSS engine. If you are a Less fan, you can specify --css less. Not specifying the --css option will default to the plain vanilla CSS we all are familiar with.
Then install the dependencies for the app:
$ npm install
That will install a bunch of modules used by the app. With that the base of the app is ready. It is already a working app. Let's see what it outputs. Start the app:
$ node app
Express server listening on port 3000
Then load http://localhost:3000/ in your browser. You will see a simple webpage with the title "Express", which looks something like this:

So looks like the app is working. Time to find out how it works.
Request flow in Express
This is how a request to an Express server flows:
Route → Route Handler → Template → HTML
The route defines the URL schema. It captures the matching request and passed on control to the corresponding route handler. The route handler processes the request and passes the control to a template. The template constructs the HTML for the response and sends it to the browser.
The route handler need not always pass the control to a template, it can optionally send the response to a request directly .
To understand how Express works, let's get working on the Ninja Store app.
Setting up the Routes
We will modify some of the stuff Express generated for us to make it more suited for our app, and more logical so that you can understand the inner workings of Express better.
Rename the index.jade file in the views folder to home.jade. This is actually a part of setting up views in Express.js, I will get there in the next section.
Delete the index.js and user.js from the routes folder. We don't need them and their presence could be potentially confusing. Create a new file called store.js in the routes folder, and put the following code in it:
exports.home = function(req, res){
res.render('home', { title: 'Ninja Store' })
};
Then we are going to modify app.js:
Delete these
, routes = require('./routes')
, user = require('./routes/user')
Add the following right before var app = express();.
var store = require('./routes/store');
Delete these
app.get('/', routes.index);
app.get('/users', user.list);
and add this
app.get('/', store.home);
With that we have set up our own route for the home page.
Routes are URL schema for a website. In Express you define them using app.get(), app.post(), app.delete() etc. The get, post, delete methods are derived from their corresponding HTTP verbs.
So far we created a single route for our app. Very soon we will be creating more, but before that I'll tell you about the files in the routes directory.
The routes directory is a convention, not a compulsion. In the routes directory we create appropriately named files which will handle the routes we define in the app.js file. We will import these files into our app and assign the functions defined in them to handle various routes.
The imported file becomes sort of like an instance of the class of the route handler file. In our case ./routes/store.js is the class, store is instance, and store.home is a method of the instance.
Again as a recommended convention, we create an appropriately named variable for the imported file from the routes directory. Then we pass one of its functions as the second parameter for the route. For eg:
app.get('/', store.home);
From that you might probably guess, we can pass any function as the second parameter for the route. You are correct, even this would work:
app.get('/', function(req, res) {
res.render('home', { title: 'Ninja Store' });
});
We don't need to render HTML pages either:
app.get('/', function(req, res) {
res.send('Look ma, no HTML!');
});
Now run the app again and see what you get. Title has changed to "Ninja Store" and you see:

We are making progress. Time to spice up the home page
Rendering Views
You have seen res.render() and res.send() in action already and probably have a fair idea about what they do. res.send() will directly send a string of text to the browser and res.render() will render a Jade template.
While it is possible to create a website entirely using res.send(), it is certainly not recommended to do so, because you will only end up with a complex and dirty looking codebase. Jade is the default templating engine for Express. Let's find out the basics of res.render() and the Jade templates.
Open the file named home.jade in the views directory. Let's examine its content:
extends layout
block content
h1= title
p Welcome to #{title}
extend layout means, this view file should look for another view file named layout.jade in the same directory and fill in the block placeholders defined in layout.jade.
Now, let's take a look at the contents of layout.jade.
doctype 5
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
It does indeed have a block named content, the content for which is defined in home.jade. Note, you can use anything for block names; "content" is just a convention.
What you are seeing is Jade code. The very basics of Jade is this:
i. HTML tag names to create tags, but without the angular brackets
ii. Spaces or tabs for indentation, but don't mix them
iii. # to assign an id to a tag
iv. . to assign a class to a tag, or create a div with a class if not already created
v. (property='value') to create attributes and assign values to a tag
Jade is out of scope of this tutorial, so you might want to read more about it here. However, as we proceed with the tutorial, I will explain you the relevant Jade code we come across.
The doctype 5 you see in layout.jade is doing a doctype declaration of HTML5. Also notice the relatively 'cryptic' title= title, the code is explained below.
title= title: The view expects a variable called title from the route handler. The variable will be set as the content of the title tag AFTER escaping special characters. To not-escape, use title!= title.
You can send data to views via the variables object in the res.render() method. Eg:
res.render('home', { title: 'Ninja Store' });
The title variable can be accessed using =title, !=title or #{title} in the views.
p Welcome to #{title}
p= title
p!= title
By default res.send() and res.render() send the HTTP status code of 200. You can specify your own HTTP status code.
Custom status code example for res.send():
res.send('File not Found', 404);
Custom status code example for res.render() (make sure you have created a file named 404.jade in the views directory):
res.status(404).render('404', { title: 'File not Found'});
Ok, now that we know the basic of views, let's revamp our homepage!
Put the following code in the home.jade file:
extends layout
block content
#wrapper
#logo
img(src='/images/logo.png')
#display
#login
form(method='post')
| Enter your name if you want to be a ninja
div
input(type='text', name='username')
input(type='submit', value='Log In')
#footer
div Copyright © Ninja Store #{+new Date().getFullYear()}
a(href='/page?name=about') About
| |
a(href='/page?name=contact') Contact Us
Make sure you create a nice logo for the store, name it logo.png, and keep it in the /public/images/ directory.
Put the following code in the style.styl file in the /public/stylesheets/ directory:
body
padding: 0
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif
a
color: #0069FF
#wrapper
width: 450px
margin: 0 auto
padding: 40px 20px
background: #fff
#logo
text-align: center
#display
margin: 20px 0 50px
#userbar
margin-bottom: 10px
Aren't we supposed to edit the style.css file? Well, style.styl is the Stylus file which generates the style.css file. Even though we code our CSS in the .styl file, we always include the .css file in the Jade template:
link(rel='stylesheet', href='/stylesheets/style.css')
So what is the point of using Stylus? Stylus offers a dynamic and efficient syntax for generating CSS. The details is out of scope of this tutorial, you can learn more about Stylus here.
Note: just like Jade templates, make sure you either uses spaces or tabs consistently for indenting your Stylus code.
Changes made to the views and everything in the public directory will be updated immediately when you refresh the browser. Refresh the browser and see what you get. In rare cases, the view may not be update - feel free to restart the server.
Wow! There you have, a log in form.

Try logging in.
Meh! Hit by an error:
Cannot POST /
Let me explain what's going on.
Remember the route
app.get('/', store.home);
we created?
That was for GET requests to the root of the website. Since our form uses the POST method, the route is not capturing it. We need to create a POST router for the home page to process the form.
Handling Forms - POST, GET Requests
Add this to app.js:
app.post('/', store.home_post_handler);
Now we need to define the home_post_handler() function in store.js module. Add the following to store.js:
// handler for form submitted from homepage
exports.home_post_handler = function(req, res) {
// if the username is not submitted, give it a default of "Anonymous"
username = req.body.username || 'Anonymous';
// store the username as a session variable
req.session.username = username;
// redirect the user to homepage
res.redirect('/');
};
So handling POST data is pretty easy - just look for them in the req.body object.
Refresh the browser.
You still get Cannot POST /. That's because for changes you make in .js files to take effect, you need to restart the server. So restart the server and try logging in again.
This time the form submission works fine, but you still see the log in form. Did we log in or not? We did log in, but our route handler made it confusing. We'll change it now. Edit the exports.home() function:
// handler for homepage
exports.home = function(req, res) {
// if user is not logged in, ask them to login
if (typeof req.session.username == 'undefined') res.render('home', { title: 'Ninja Store'});
// if user is logged in already, take them straight to the items list
else res.redirect('/items');
};
Restart the server and log in again.
Cannot GET /items
An error again. But it looks more promising this time, the app is trying to load an undefined route. If we define the route, we would have it working.
Before we get to defining the missing route, let's optimize the views a little bit. Some components of the view will be common to all the views, so it is a good idea to modularize them. The Jade template engine has a command called include using which you can include files into a view.
Create a file called footer.jade in the views directory with the following content:
#footer
div Copyright © Ninja Store #{+new Date().getFullYear()}
a(href='/page?name=about') About
| |
a(href='/page?name=contact') Contact Us
Create a file called userbar.jade in the views directory with the following content:
#userbar
| Welcome
b #{username}
| |
a(href='/items') Items
| |
a(href='/logout') Log Out
Now edit the home.jade file:
extends layout
block content
#wrapper
#logo
a(href='/')
img(src='/images/logo.png')
#display
#login
form(method='post')
| Enter your name if you want to be a ninja
div
input(type='text', name='username')
input(type='submit', value='Log In')
include footer
We will be defining the missing items/ and a related route in a while. In the process, we will also find out how to handle GET requests in Express.js.
There are two types of parametric GET requests in Express.js - req.params and req.query.
The req.params object contains request parameters right in the URL. It uses the clean URL scheme. Eg: http://example.com/product/1274.
The req.query object contains request parameters in the GET query. It uses the conventional GET request scheme. Eg: http://example.com?product=1274.
For displaying the items we will use the req.params method.
Add these routes to app.js:
// display the list of item
app.get('/items', store.items);
// show individual item
app.get('/item/:id', store.item);
/items for listing the items of the store.
/item/:name for displaying the individual items.
We will use a simple hard-coded array as the data source. Add the following to the store.js file.
// our 'database'
var items = {
SKN:{name:'Shuriken', price:100},
ASK:{name:'Ashiko', price:690},
CGI:{name:'Chigiriki', price:250},
NGT:{name:'Naginata', price:900},
KTN:{name:'Katana', price:1000}
};
Now create the corresponding route handlers for the routes we added. Add the following to store.js:
// handler for displaying the items
exports.items = function(req, res) {
// don't let nameless people view the items, redirect them back to the homepage
if (typeof req.session.username == 'undefined') res.redirect('/');
else res.render('items', { title: 'Ninja Store - Items', username: req.session.username, items:items });
};
// handler for displaying individual items
exports.item = function(req, res) {
// don't let nameless people view the items, redirect them back to the homepage
if (typeof req.session.username == 'undefined') res.redirect('/');
else {
var name = items[req.params.id].name;
var price = items[req.params.id].price;
res.render('item', { title: 'Ninja Store - ' + name, username: req.session.username, name:name, price:price });
}
};
Time to create the views for the routes.
View for listing all the items on the store. Name it items.jade.
extends layout
block content
#wrapper
#logo
img(src='/images/logo.png')
#display
include userbar
-for (var id in items)
- var item = items[id]
div
a(href='/item/#{id}') #{item.name} - $#{item.price}
include footer
View for listing individual items of the store. Name it item.jade.
extends layout
block content
#wrapper
#logo
img(src='/images/logo.png')
#display
include userbar
p The #{name.toLowerCase()} is one of the must-have items for any aspiring ninja. It costs just $#{price} on our store.
p Buy it today!
include footer
Our footer links uses the conventional GET style links, let's find out how to handle those kind of requests.
First create the routes for the footer links. Add the following to app.js.
// show general pages
app.get('/page', store.page);
Now, the route handler in store.js:
// handler for showing simple pages
exports.page = function(req, res) {
var name = req.query.name;
var contents = {
about: 'Ninja Store sells the coolest ninja stuff in the world. Anyone shopping here is cool.',
contact: 'You can contact us at <address><strong>Ninja Store</strong>,<br>1, World Ninja Headquarters,<br>Ninja Avenue,<br>NIN80B7-JP,<br>Nihongo.</address>'
};
res.render('page', { title: 'Ninja Store - ' + name, username: req.session.username, content:contents[name] });
};
And then the view for the pages. Create a new view file named page.jade in the views directory with the following content.
extends layout
block content
#wrapper
#logo
a(href='/')
img(src='/images/logo.png')
#display
p!= content
include footer
Notice how we use p!= content instead of p #{content}. That is because we don't want the Jade engine to escape the HTML content of the variable.
we need to create a route and a handler for logging out. Add this route to app.js:
app.get('/logout', function(req, res) {
// delete the session variable
delete req.session.username;
// redirect user to homepage
res.redirect('/');
});
Notice how we specified the route handler right in the app.js file with the route definition. All the route definition cares about is that the second parameter should be a function, wherever it may be defined; just make sure to pass the request object (req) and the response object (res) to it.
With that our Ninja Store website ready. Start the app and get ready to witness the brilliance of the Ninja Store in action.

Conclusion
You know what? The whole project is on GitHub
I intentionally didn't tell you earlier, so that you don't get lazy and skip learning. Now that you done the hard work, feel free to clone the Ninja Store repository and go berserk on it.
Express.js is a lot more than what I covered in this tutorial. I have already, and will be covering the more advanced topics in dedicated articles on my website.
Subscribe to my RSS feed and follow me on Twitter for excellent articles on Node.js and Express.js.
I found a typo in line: app.post(‘/’, store.home_post_handler);
You mean “routes” instead of “store”.
Great tutorial. I’m starting with Node & Express and this introduction is being really good.
Arr, not really. Refer: https://github.com/hacksparrow/ninja-store/blob/master/app.js
Look at line no. 7.
storeis our module for handling the routes.Oh, thanks and all the very best with your Node-Express endeavor, Sergio!
You are right. Forget it
Congrats again.
Thanks. It’s very helpful for me.
How can I used raw HTML in jade file?
rawHTML in jade file: I got it
https://github.com/visionmedia/jade#a6-10
Glad you found it helpful. I liked the Elegant Universe too, Dr. Greene
That is just HTML text. To include a .html file:
include file.html
This is a great toot… it fast tracked me w/ nodejs and friends… I really like the simplicity and speediness… thanks again, you are a great sensei!
Greg
The pleasure is mine, Greg. I hope you become a billionaire selling Ninja stuff.
- Captain
Dude this was the best ExpressJS tutorial I’ve ever seen! Everything actually worked… I’m amazed. Oh how I wish there were more of this greatness in here as I’m a blunt instrument on this bleeding-edge technology. =)
Hey Ryan, party on dude!
Thanks for a great tutorial. This really helped me get going on node.js and express. Next up: trying to connect to mongodb, and publish some data from it. For this, I also need to find out how to structure my code, so I don’t have to declare all the DB-stuff in every module. I’m sure there’s a way for this.
Thanks again, man. This was a great help!
Hi Gorm, glad you found it useful. I write a lot about MongoDB too. Here is how to get started with MongoDB + Node.js – http://www.hacksparrow.com/mongodb-with-node-js.html
No flaws or typos stopping you dead, no complicated structures scaring off beginners.
Best tutorial I’ve found on any framework.
great Tutorial, please keep on
Thanks for the in-depth tutorial; I successfully got the ninja store up and running. However, on Internet Explorer 9, if I click the back button, pages do not render with the CSS applied to it. Latest Firefox and Chrome render fine. Any idea why this happens in IE?
dude you freakin rock. I look forward to doing some of ur other node tuts. Thanks for keeping it simple.
\m/
I have a big problem with this. In the “home_post_handler” function the line “req.session.username = username” throws an exception. As far as I can tell, neither the request, nor the response objects contain a session field.
What am I doing wrong?
After a bit of digging around I found the solution. In “app.js” I did not have the following two lines of code:
app.use(express.cookieParser());
app.use(express.session({ secret: “your secret here” }));
Since other people do not seem to be suffering from this problem I can think of three possibilities for this:
(1) The latest version has stopped putting this in automatically;
(2) You are all using Linux and there is a difference on Windows (seems unlikely);
(3) Everyone has gone straight to the code on github rather than typing everything in by hand.
Any ideas? You might want to mention this in the requisite section, just to make your tutorial even easier
Other than that – very good tutorial so far. Thanks.
Hi Alex, yes you need to enable cookieParser. While creating the project make sure you don’t miss the
--sessionsoptions, else you will need to edit app.js yourself.Nicely done, thanks.
At first, I think we can use “express ninjia-store –sessions –css stylus”, instead of seperating commands.
Hey Brain, I wanted to make this tutorial very noob friendly, hence the detailed instructions. But I have updated it with your suggestion on the next para.
Hi Sparrow Captain,
It’s me again, thanks for updating the post and adopting my suggestion.
Your articles are awesome, hope to read more from you. Keep up good work.
I’ll be great if you can help us look into the mvc setup from Express example: https://github.com/visionmedia/express/tree/master/examples/mvc.
Thanks.
Hi Brian, thanks. Sure I will take a look into the MVC example.
Great tutorial Sparrow, really got the just of express with this one!
Love this site man
Hi Capt,
I am trying to upload this app to nodester without any success, can you help?
Hi Tony, I have no idea about Nodester. Maybe you could ask someone here – http://irc.nodester.com/
Thanks anyways Sparrow, love this blog mate!
Thanks bro, I hope to make it even more awesome.
Thanx Captain! Nice Tut.
Great explanation. Works fine
Great. All the best, Martin!
Hello Captain,
this was and is farewell the best tutorial on the web concerning an entry to ExpressJS. But since the latest version of jade and express, not everything is working out fine. You now have to use “extends layout” in your template files as well as some other little tweaks to get the app running.
Could you do some rework on the tutorial to be up to date? I guess many people would appreciate that.
Greetings from Germany!
Got me up ‘n running in a chinch. Thanks.
Hi there Wegginho, since ExpressJS 3 is not officially out yet, it would be disruptive for users new to Express (v 2.x). Once ExpressJS 3 is released I would publish a new tutorial or update the existing one. I working on an Express 3 tutorial already, btw.
Nice tut!
Greetings from Tahiti ; )
@jpcweb, beautiful country! (y)
Hi Captain! Great tutorial help me a lot!!
It would be perfect if you provide more tutorials, thanks!
Thanks Captain, it really helped a lot for me to understand Node, express, jade, and Stylus. I made a git repo using your tutorial, and added Mocha tests a bit. Any corrections/advices to the test codes are welcome.
https://github.com/philipjkim/ninja-store
Hi Captain,
I followed the code you made, but when I modified the store.js file as a new name distribute.js, it can not work, the error showed like below. Could you please let me figure it out? I have double check the other file code, they were the same as the one you did.
ReferenceError: distribute is not defined
at Object. (/Users/Samuel/Summer/node/app.js:35:14)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:38)
@samuel, probably you forgot to do this:
var distribute = require('./routes/distribute');@philipjkim, good job bro!
can i use same app.js for multiple file in veiws?
if not possible how can i use?
great stuff..everything is explained so well and clearly..now I start liking node and express…thank you so much!
Did this tutorial still work with the new version of express?
I see that you are making a new tutorial to cover express3 woot woot!
can’t wait to see it captain
Great stuff.. any news on when the version for Express 3 will be done?
Really great Tutorial. Thanks for taking the time for this.
A couple of hopefully positive criticisms:
- the tut speeds up a lot towards the end with tons of changes and no chance to test if the changes were working until the end. Could perhaps do with a few more steps with more detailed explanations.
- probably my fault but the changes towards the end broke the tutorial and I couldn’t figure out why. I am guessing it was likely my fault by making an incorrect change, hence my request for more steps towards the end.
I got this to work with express3 by making a few fairly trivial changes to app.js and after copying your code exactly from GitHub.
Hi Sean, I am working on a new Tutorial for Express 3, I will consider all your feedback. Thanks!
Many thanks for this. Great tutorial.
Great tutorial!
Thx~
I need your help please contact via e-mail, i need a express copy with all dependences, i can’t use npm !
Un buen tutorial como para compartir..
Great Express tutorial. I would update to strict equality checks for the session variable username.
typeof req.session.username === ‘undefined’
instead of
typeof req.session.username ==’undefined’
I do like the compactness of including a single routes file and accessing via store.contact, store.item,… versus page specific route files: contact, item,… when the routes are less involved.
Great Job.
Thanks heaps! This saved me a lot of reading and got my head in the code.
You provided enough information to make it easy to follow along while getting results.
Just what I needed, much appreciated.
Great Post man.. it really helped me to get started off with express and node.. thanks a lot.
This is an awesome express tutorial! Best I’ve seen so far. Could you explain how app.post() works, or point me to where I could learn?
Hi Bob, check this out: http://www.hacksparrow.com/post-get-request-handling-in-node-js-express.html
Wow!!! Thanks bruv for this!!! You are great
it’s a easily understandable tutorial,great job.i find some small errors in the article.
“body!= body: The template expects a variable called body from the route handler which called the red.render() method.”
should be res.render()
Thanks liu, fixed.
I tried to get Express running but at “node app” got the following;
MacBookPro-xxxxxxxxxxA-2:ninja-store rprancan$ sudo node app
Password:
events.js:66
throw arguments[1]; // Unhandled ‘error’ event
^
Error: listen EADDRINUSE
at errnoException (net.js:768:11)
at Server._listen2 (net.js:908:14)
at listen (net.js:935:10)
at Server.listen (net.js:984:5)
at Object. (/Users/rprancan/Work/Projects/ninja-store/app.js:36:24)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
Any idea????
That means the app port (3000?) is already being used by some other process.
awesome tutorial with great explanation!
Thanks a lot for writing this..
PS- eagerly waiting for Express 3 tutorial!
Hi noder, thanks! Express 3 tutorial on the way!
aye, aye Captain!
thanks this was few tutorials which really work
really great if you can also make a video
I think so too
but it’s gonna be a while if I have to do it.
Hey there Captain,
I’m going over your tutorial and doing everything step-by-step to make sure I got everything down, however, I do everything in coffeescript to make sure I’m really understanding the logic as I type everything out
I have one question for you, at the moment: I cannot get the req.session.username = username to work, as I constantly get username is not defined.
Here’s how I’ve structured my data for the handler and obviously, the .jade file is the same as yours.
module.exports =
home: (req, res) ->
res.render ‘home’,
title: “Express Test”
homePostHandler: (req, res) ->
username = req.body.username
req.session.username = username
res.redirect ‘/’
Any tips? I assume that req.session.username is something that is being extracted from the DOM since req.body are part of the req object, but not sure how/where username is coming from (somewhere I define it (the jade input form? or simply part of the req object?). Also, the same question for req.session.username? Is session part of the req object or was it defined somewhere? and how is username associated with that?
Thanks in advance for your help!
I got the code above to work after looking around and seeing that the new Express does not include these two files in the app.js:
app.use express.cookieParser()
app.use express.session secret: ‘your secret here’
Would you be able to explain what these are and what they were taken out in the newer version? I couldn’t find anything intelligible. Also, I’m wondering what’s that “your secret here” message (need to be used in a particular way?).
Many thanks in advance for your help!
Hi FT,
app.use(express.cookieParser('your secret here'));
app.use(express.session());
is required if you want to support sessions in your app, which is enabled by using the
--sessionsoption with theexpresscommand. Looks like you did$ express
instead of
$ express --sessions --css stylus
Also, I would not recommend CoffeScript for any app that is even slightly complex.
Nice tutorial
Thanks, Maurico. There is a lot more for Node and Express developers on my website, so explore around.
Thanks for the response, Captain!
Yes, I must have overlooked that during the Express init. Question for you: why are you advising against CoffeeScript for advanced projects? Curious…
CoffeeScript, as such, is a cool concept and its syntax is beautiful, but it is a pain when it comes to debugging. CoffeeScript would make a wonderful language on its own, but I am not a fan of it as a wrapper on top of JavaScript. Learn JavaScript, you will have more control over your apps.
Great point! I haven’t got to these complex applications, yet, but I can see what you mean.
One question for you: when you logout from the item.jade, there seems to be an error. It doesn’t redirect to the localhost:port/ but rather to the parent (e.g. localhost:port/items/) which throws an error.
@FT, the project is on GitHub – https://github.com/hacksparrow/ninja-store – compare your files with it.
Great tutorial… Thanks…
thank you for this helpfull tuto!
there is a few indentation problems for home.jade (#container) but github files correct that.
now I’ll read some other tuto on your site
Thanks Dave. All the best!
Any news on an updated tutorial? I had scoured the web for a good ExpressJS tutorial for a beginner like me, and yours was the only one I could follow. Everything worked out great until I got to the routes, and I assume because I’m using a newer version of Express.
@Ryan, I will find some free time these days and complete the new Express tutorial. Been kinda busy.
arrrrrrrr hey captain, I have a small problem, I dont see any of the css properties in my localhost:3000 right after I “Put the following code in the style.styl” part of the tutorial
what am I missing?
nevermind, I fixed it, it was an indentation problem
Hi Captain!
I wanted to learn to use NodeJS and Express with this tutorial, but i got stuck in the “routes” part. In the comments i saw why, im using Express 3 and this tutorial is for Express 2. Anyway i read all the tutorial and it was awesome. Im waiting for the Express 3 tutorial! This was pretty awesome.
Greetings from Venezuela!
Hi Roger and everyone else, looking for the Express 3 tutorial. I know it is about time, I updated this. I am on it as I type this response.
Hola Roger, Express and Node are pretty cool. All the best!
Tutorial updated for Express 3! Harrrrr!
WOW! That was quick!
I’ll be following this tutorial on the weekend.
Thank you very much Captain!
Excellent tutorial to jumpstart express.
Thanks Roger and Alfred. All the best!
Hey Captain, im looking forward on learning Javascript. Any good tutorial you recommend?.
Your blog is awesome, keep on the good work!
Hi Juan, if you already have some programming experience, I recommend https://developer.mozilla.org/en-US/docs/JavaScript/Guide. If you are totally new to programming, you can start with http://www.w3schools.com/js/default.asp. Good luck!
Thanks Captain!
Hi Captain. Great tutorial! Helped a lot to understand the basics. Now, I’m looking for a tutorial on integrating a 3rd party api client library with node + express. Having a struggle with this RunKeeper API: https://github.com/mowens/runkeeper-js.git. I’m not clear on the architecture and flow for the basic authentication steps and how this is relevant to the client library. Any similar tutorials you can recommend around this concept?
Great tutorial. Trying to understand how to integrate API client libs like this:
https://github.com/mowens/runkeeper-js.git.
Can’t find anything on the web. Any recommendations?
AZ, I can understand why you are having a tough time using that module. It seems to be because of a lack of the basic knowledge of how Node modules work. Read this up:
http://www.hacksparrow.com/how-to-write-node-js-modules.html
You will have a good understanding of how Node modules work and how to use them.
I found a typo in the following command:
$ express ninjia-store –sessions –css stylus && cd ninja-store
It should be “ninja-store” rather than “ninjia-store”. Great tutorial by the way
Could you do a tutorial on websockets and how to use them with Express.js?
I got blow message at console:
GET /stylesheets/style.css 304 0ms
I need help ~
Thanks Mart1, fixed. WebSockets + Express, yes, one of these days.
alexis, I need more details to help you.
Hi, Captain
I use “express ninja-store –sessions” command to install. (I’m not use stylus)
and
>cd ninja-store
>node add.js
It shows:
GET / 200 421ms – 170b
GET /stylesheets/style.css 200 15ms – 117b
It works fine.
but when I delete blow at app.js:
, routes = require(‘./routes’)
, user = require(‘./routes/user’)
app.get(‘/’, routes.index);
app.get(‘/users’, user.list);
and add : (at appropriate place)
var store = require(‘./routes/store’);
app.get(‘/’, store.home);
restart node app.js and browse http://localhost:3000/
it shows:
GET / 200 390ms – 182b
GET /stylesheets/style.css 304 0ms
I don’t know why, help T_T…
PS: my Express version is 3.1.1
Yes, I
–Rename the index.jade file in the views folder to home.jade.
–Create a new file called store.js in the routes folder, and put the code in it
the issue is still …
Oh, I think it’s normal..
HTTP Status code 304 means Not Modified.
When you installed the app, and run app.js
First browse web, it will show below at console:
GET / 200 31ms – 170b
GET /stylesheets/style.css 200 16ms – 121b
and use F5 to reload the web
it’s show below at console:
GET / 200 0ms – 170b
GET /stylesheets/style.css 304 15ms
the browser read the css file at cache ? @_@…
GREAT tutorial… just telling you that launching the app.js with “nodemon” you dont have to reset de server, it listen to changes you make all the time
> nodemon app.js
I hope its helpful to someone
Thank you again for the tutorial… in the end,
I’ve just finished this; thanks so much for this free, high-quality tutorial!
I originally started trying to work through the previous (Express 2) version but since I had express 3 installed and you had a comment that you were planning to update it, I sat tight. It was well worth the wait.
how can you determine a longer session period?
in my case it stays only something like a minute… how can you set a session of 24h for example?
thanks!!
Brilliant tutorial, Captain! Absolutely the best explanation on the interweb about routes and views in Express – and I’ve done a LOT of searching.
Superb choice of sample app, with clear and comprehensible commenting, and easy-to-follow steps. Thanks for spending time and thought to get it to such a high standard!
@miguel, pass an option object to the session middleware with the cookie settings –
app.use(express.session({cookie: {maxAge: (24 * ms_in_hour)}}));. For more details – http://www.senchalabs.org/connect/session.html.@JohnMiller, thanks! Glad to know you found it useful.
Thanks Captain!
Go on with the good job, I think we all thank you
))
A great tutorial to start with ! Thanks.
1 question here , if i want to use this kind of app with tomcat server or existing java web application then what i need to make changes ? Any pointers will be appreciated .
Piyush, I am not getting your question. Maybe you can rephrase it.
Hi Captain,
I was looking for code snippet which i can use with java program running on apache tomcat server.
We can use your app without any server like apache tomcat / jboss as it uses its own server. Can we deploy it in tomcat / jboss server ? What changes we need to do in your program ?
Hope this clarify my question.
@Piyush, you could implement a http proxy in Java and put it in front of your Express app.
Can you give me some link of tutorial which i can refer in that ? I am newbie in express.js/node.js
Great work! I am new to node.js, express, jade, stylus and this tutorial explained things in a clear concise manner. Thanks for the start!