Skip to main content

Posts tagged with 'express'

I got my "Hello Express" example working on Docker for Windows. I see the potential benefit of putting a (node) app in docker, but am I meant to actually develop this way? It seems like a lot of steps to create a package each time I change code. There must be something I'm missing: please leave a comment to let me know.

I also took the time to update the version of Node I was using. It was painless: I uninstalled the old version and used choco install nodejs. This installed node 6.2.2 (code named "slim"?) and npm 3.9.5. I ran my hello world and hello express code, and they still worked fine.

To start with, I created a brand new express project:

express hello-express-docker
cd hello-express-docker
npm install

And then, I opened it with Visual Studio Code.

code .

By the way, you can open up a console window from within Visual Studio Code. View -> Toggle Integrated Terminal (or Ctrl+`).

Next, I pretty much followed the instructions on how to Dockerize a Node.js web app. First, I created a Dockerfile in the hello-express-docker folder. This is just a text file named "Dockerfile". Inside that file, I put:

# I used node:slim which corresponds to 6.2.2 I guess
FROM node:slim

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

# Expose the port

# kick off the express site
CMD [ "npm", "start" ]

Note that I used a different port number than the instructions, because that's what express's default seems to be (not 8080 as the instructions indicate).

Next I created a docker package.

docker build . -t ccc/hello-express-docker

The tag isn't strictly necessary; the ccc stands for "Cross Cutting Concerns" :)

Once that's done, you can list all the images you have with:

docker images

I have some other images that I downloaded from the docker hub, including couchbase, nginx, ubuntu. You should see at least the ccc/hello-express-docker package listed.

Now that I have an image, I'll run it just like any other docker image:

docker run --name hello_express_docker -p 49160:3000 -d ccc/hello-express-docker

Port 49160 is just an arbitrary port. Once that's running, I can visit my site in a browser: http://docker:49160 (docker is the host name that Docker for Windows uses. If you are using Mac, Linux, Docker Toolbox, etc that may vary).

And that's it! Now my hello-express site is running in a container. If I were done, I guess I could give this container to devops for deployment. I'm struggling to see why I would do this on a day-to-day basis as a developer. Maybe I can have the docker container "mount" my working folder so I can make changes with Visual Studio Code. Or vice versa?

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

I'm trying to familiarize myself with node.js, since it seems to be in use by a lot of Couchbase customers. I heard that Visual Studio code had some pretty good node.js support built in, so I decided to give it a shot.

I already had node and npm installed on my Windows machine. I don't remember why. If you aren't sure if you do, open up Powershell (or cmd) and try:

node --version


npm --version

(If metaphors help you, I think of node as ASP.NET and I think of npm as NuGet)

If neither of those commands works, then you can install node with Chocolatey NuGetchoco install nodejs

The next easiest thing you can do is create a file with text editor, say "app.js". Put this into that file:

var msg = "Hello, world!";

Woo. Now back at the command line, type:

node app.js

And guess what it will write out? If you guessed "Hello, world!" then give yourself 10 points.

So, what good is this? Well, you can now write programs in JavaScript outside of a browser. A lot of people use this to write server-side JavaScript, for example. They might use a framework like Express (which, back to the metaphors, I think of as ASP.NET MVC). You can use npm to install express and an npm "start" command to run your Express app. I may blog more about that later, after I play with it some more.

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