Skip to main content

Posts tagged with 'npm'

Node code - hello express

July 01, 2016 mgroves 0 Comments
Tags: node express npm

In a previous post, I created a "Hello, world" with node.js.

Now, I'm going to do something a bit more substantial. I'm going to see if I can get a website running on node.js with the Express.js framework.

Why Express? Because it's the "E" in all those "*EAN" stacks (e.g. CEAN: Couchbase-Express-Angular-Node). One of my former coworkers suggest that I also try the hapi framework, which I just might do. Later.

Okay. On to Express. I already have node and npm installed (though it looks like I have a pretty old version; maybe I will update soon).

The first thing I did was to use npm to install the express-generator. This isn't strictly required, but it seems like roughly the equivalent of "File->New->MVC" in Visual Studio. I believe this only needs to be done once, and not for every project. In Powershell:

npm install express-generator -g

The -g makes it global, so I don't have to install it again to start a new project the next time.

Then, still in Powershell, I pick a root folder (I call mine "zproj", maybe you call yours "work" or "projects" or something). To kick off express generator, I enter:

express hello-express

This creates a subfolder called hello-express. I go into that folder and enter:

npm install

I'm not exactly sure, but I think this is roughly the equivalent of a NuGet package restore.

The express generator creates a couple of "routes" out of the box for me (this seems to be roughly equivalent to an ASP.NET MVC Controller). There's an 'index' and there's a 'users'. To kick off the site, I enter:

npm start

If you look in package.json, you'll see that "npm start" is a kind of shortcut for "node ./bin/www". This will spin up a web server on localhost, port 3000. When I visit that in a browser, I see this:

And that's that. What's next? Maybe I'll play around with Express and try to wire it up to Couchbase. Or maybe I'll try to get this working in a Docker container. Or maybe I'll try hapi. Or maybe I'll try updating the version of node I have and see how that goes.

By the way, what I showed you here is pretty much lifted directly from the getting started guide on expressjs.com.

Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments

Twitter