Loading...
Loading...
Marco Franssen

UPDATE: I updated the console output and Express example to the versions available at the time of this update.
Let's build a simple Node.js web server with Express. Before we start our first project, I'll explain what Node.js is. Rather than reinventing the wheel, here's a quote from Node.js itself.
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Next, you should know about npm, the Node.js package manager. With npm, you can use thousands of open-source packages instead of building everything yourself.
I'll assume you've already installed Node.js. Let's start with a simple Hello World application: create a file called hello-world.js, then run it to see the result.
console.log("Hello World");To run the script, navigate to the folder containing hello-world.js and execute it with node, as shown below. On Windows, open the Node.js command prompt.
Your environment has been set up for using Node.js 4.1.1 (x64) and npm.
Press any key to continue . . .
C:\Users\Marco> cd MyHelloWorldCodeFolder
C:\Users\Marco\MyHelloWorldCodeFolder> node hello-world.js
Hello WorldOK, we've just seen our very first Node.js application running! Before we move on, I'd like to install nodemon, a package that's handy during server development. It monitors our working directory and automatically restarts the Node.js server when we change the code. Since I want to reuse it across my Node.js projects, I'll install it globally with the -g command-line option.
Your environment has been set up for using Node.js 4.1.1 (x64) and npm.
Press any key to continue . . .
C:\Users\Marco> npm install nodemon -gNow we can use nodemon to run our scripts. Let's test it by starting the script with nodemon, then changing the script.
Your environment has been set up for using Node.js 4.1.1 (x64) and npm.
Press any key to continue . . .
C:\Users\Marco> cd MyHelloWorldCodeFolder
C:\Users\Marco\MyHelloWorldCodeFolder> nodemon hello-world.js
23 Nov 18:49:46 - [nodemon] v0.7.10
23 Nov 18:49:46 - [nodemon] to restart at any time, enter `rs`
23 Nov 18:49:46 - [nodemon] watching: C:\Users\Marco\MyHelloWorldCodeFolder
23 Nov 18:49:46 - [nodemon] starting `node hello-world.js`
Hello World
23 Nov 18:49:46 - [nodemon] clean exit - waiting for changes before restartOpen another console so we can install our first local package. We'll use colors to give our console output some nice colors.
Your environment has been set up for using Node.js 4.1.1 (x64) and npm. Press any key to continue . . .
C:\Users\Marco> cd MyHelloWorldCodeFolder
C:\Users\Marco\MyHelloWorldCodeFolder> npm install colorsThen change hello-world.js to the following.
var colors = require("colors");
console.log("Hello ".red + "Node.js".green);We require the colors package in our script so we can use it. Check your console, and you'll also notice that nodemon has automatically restarted the script.
23 Nov 18:50:05 - [nodemon] restarting due to changes...
23 Nov 18:50:05 - [nodemon]
C:\Users\Marco\MyHelloWorldCodeFolder\hello-world.js
23 Nov 18:50:05 - [nodemon] starting `node hello-world.js`
<span style="color: #ff0000">Hello</span> <span style="color: #008000;">Node.js</span>
23 Nov 18:50:06 - [nodemon] clean exit - waiting for changes before restartNow our development environment is set up, and we can easily develop and test the script. Since I don't like reinventing the wheel, we'll use Express to build our web server. First, install it with npm.
Your environment has been set up for using Node.js 4.1.1 (x64) and npm. Press any key to continue . . .
C:\Users\Marco> cd MyHelloWorldCodeFolder
C:\Users\Marco\MyHelloWorldCodeFolder> npm install expressNow we can use Express in hello-world.js. Let's make some changes and watch nodemon automatically restart the script and launch our web server.
var express = require("express"),
path = require("path"),
app = express();
app.set("port", 3000);
app.use(express.static(path.join(__dirname, "/public")));
app.listen(app.get("port"), function () {
console.log("Express server listening on port %s.", app.get("port"));
});23 Nov 19:13:01 - [nodemon] restarting due to changes...
23 Nov 19:13:01 - [nodemon] C:\Users\Marco\MyHelloWorldCodeFolders\hello-world.js
23 Nov 19:13:01 - [nodemon] starting `node hello-world.js`
Express server listening on port 3000.Our web server is now listening on http://localhost:3000. We've configured the public directory to serve static content. Let's add an HTML page with an awesome "Hello Node.js express webserver" message. Create a public folder, then create index.html inside it.
<html>
<head>
<title>Hello Node.js express webserver</title>
</head>
<body>
<h1>Hello Node.js express webserver</h1>
<p>
I have just created my first node.js webserver, serving static content.
</p>
</body>
</html>Open your browser again and navigate to http://localhost:3000.
You've now seen how to create a simple HTTP server that serves static content. Of course, there are other solutions for serving static files, but these first steps should get you up and running—and thinking about more advanced Node.js applications. Feel free to add client-side JavaScript, stylesheets, and images to the public folder to make your page a little fancier.
I hope this small guide was useful. Enjoy writing your own Node.js applications, and share this article with your friends. Thanks for reading!
Marco Franssen
Automate development and CI tasks with Grunt: configure a Gruntfile, run task targets, combine tasks, and watch files to rerun jobs when they change.
Marco Franssen
Learn how to initialize, install, update, and publish Node.js packages with npm in this 2013 walkthrough, including development dependencies and private packages.
Marco Franssen
Connect a .NET MSBuild CI script to Jenkins, trigger builds from GitHub, configure repository access, and publish MSpec and code coverage reports.
Marco Franssen
Extend a .NET MSBuild CI script with MSpec tests, OpenCover code coverage, and ReportGenerator reports that you can later publish through Jenkins.