I'll start with the code…
There it is. The simplest Node.js webserver/website. Three lines of code.
I know I've got it a bit mungled together. I'm not recommending this code. Just having fun.
Notice that I'm hanging a
() directly off of the
require('express'). The require statement returns a function. This is one of the most popular of the Node module patterns. Since it returns a function, hanging parenthesis off of it executes the function.
The execution of the function returns an express app, and the app supports chaining like jQuery or LINQ, so you do a
.get() and then a
.listen() and those are both methods of an express app.
.get() creates the root route (
'/') and then allows you to provide a function to handle that route. The function does a simple hello world.
.listen() starts up the engine.
node app, and you should get zero feedback. But then hit
http://localhost:3000 in your browser of choice and you'll get the reassuring "hello world".
By the way, if you're using ES2015, that gets even shorter…
I find so much value in simple examples. It's the whole reason I created codeShow.
Listen, bloggers. Most of the time, folks aren't interested in your entire awesome solution. I know it's awesome, but you have to consider the time it's going to take for people to get their heads around a) what it is you're trying to do (your domain), and b) all of the other stuff you're doing that they don't care about.
So, in the interest of keeping it simple, let's take a look at a simple Node.js app for hooking in to the excellent streaming API by Twitter. Let's take a look at it in 15 lines of code.
First, a note as to why you'd want to do this. Of course, you could do the atrocious - that is, hit twitter.com and scrape some results. Yuck. Or you could do the classic - that is, use the standard Twitter API and start asking it every 5 seconds if they have anything you're interested in yet. But why do the atrocious or the classic when you could do the fantabulous? The fantabulous is hooking into the streaming API and then letting your app just sit there waiting for Twitter to call you saying “Hey, we found something you're interested in.”
Here it is… in its entirety…
The above code has a simple dependency on a rather popular Node.js module called twitter. I'm obviously assuming you are familiar with installing Node.js modules here, but that's pretty easy to pick up.
The magic about the above code is that a) it does what it's supposed to and b) it doesn't do anything else.
The documentation for the twitter module is pretty sparse, but you don't need to know much about it. You really need the documentation for the Twitter Streaming API. That will teach you what a filter type stream is, how to add a search term as a track, and how to handle the data that is returned.
That's all for now.
(and yes, those keys are obfuscated :)
I have a lot of Node.js code on my box and occasionally I'll get into a situation when I have an existing Node.js project (one that I didn't create initially using Visual Studio), and I want to open it in VS.
When you create a new Node.js project in Visual Studio, it gives you the basic files - app.js, package.json, and README.md - and then it gives you a .njsproj file. That's the file that lights VS up with all of the additional help that's specific to Node.js. That's the file we essentially need to create in an existing project, but how? It's not exactly a new project, but according to Visual Studio's definition, it's not an existing project either.
First, open Visual Studio and choose File | New Project…
On the right, you will see a big list of project templates, and notice the second one (it's the second on my list at least) - From Existing Node.js code. That's the one. Hit it.
The values in the 3 boxes on the lower end of the box - Name, Location, and Solution Name - by the way, are used to populate default values on subsequent screens, but you'll have a chance to change them. I recommend you put the name of your project in the Name field and the path to it in the Location.
Then hit OK.
The first wizard screen prompts you for the location of your existing Node.js project. If you populated the Location field on the New Project screen, then this will already be set. The rest of this screen is self-explanatory.
The wizard next guesses what is the entry point. If it's not right then point it to the right one.
Finally, you need to indicate the name of the .njsproj file, and again it's going to pull a default from the New Project screen if you filled that out.
And there you have it. You've essentially gotten a .njsproj file created where it should be. If you created this new project from the File menu like I did in my example, then your new Node.js project is inside of an unsaved solution file, so when you close Visual Studio or the solution, it's going to prompt you to save it. The other approach - if you already had a solution - would be to open it, right click the solution file, and then choose New Project from the context menu.
Have fun with Node.js!