Node.js is an open-source, cross-platform JavaScript runtime environment for developing a diverse variety of tools and applications. Although Node.js is not a JavaScript framework, many of its basic modules are written in JavaScript, and developers can write new modules in JavaScript. The runtime environment interprets JavaScript using Google's V8 JavaScript engine.
I use Node.js at work, and find it to be very powerful. Forced to choose one word to describe Node.js, I'd say "interesting" (which is not a purely positive adjective). The community is vibrant and growing. JavaScript, despite its oddities can be a great language to code in. And you will daily rethink your own understanding of "best practice" and the patterns of well-structured code. There's an enormous energy of ideas flowing into Node.js right now, and working in it exposes you to all this thinking - great mental weightlifting.
Pros / Cons:
- Pro: For a server guy, writing JavaScript on the backend has been a "gateway drug" to learning modern UI patterns. I no longer dread writing client code.
- Pro: Tends to encourage proper error checking (err is returned by virtually all callbacks, nagging the programmer to handle it; also, async.js and other libraries handle the "fail if any of these subtasks fails" paradigm much better than typical synchronous code)
- Pro: Some interesting and normally hard tasks become trivial - like getting status on tasks in flight, communicating between workers, or sharing cache state
- Pro: Huge community and tons of great libraries based on a solid package manager (npm)
- Con: JavaScript has no standard library. You get so used to importing functionality that it feels weird when you use JSON.parse or some other build in method that doesn't require adding an npm module. This means that there are five versions of everything. Even the modules included in the Node.js "core" have five more variants should you be unhappy with the default implementation. This leads to rapid evolution, but also some level of confusion.
Now, let's install Node.js and see how it working. Go to https://nodejs.org/en/ and download the version you need (in this case we are using v6.9.1 LTS)
Create a folder and let's name it: helloNode
Launch the Command Prompt and navigate to helloNode folder. Run: npm init
This will prompt you for some information about your node app and create package.json file....see the image bellow:
So you are in \helloNode folder, in Command Promt type: npm install express --save
This command will add Express as dependency for our project. If you don't know what Express is, take a look here: Express .
After adding Express dependecies, a new folder (node_modules) is added to \helloNode. Now create a new file in \helloNode and let's name it index.js
In index.js add the following code:
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
The app starts a server and listens on port 3000 for connections. The app responds with “Hello World!” for requests to the root URL (
/
) or route. For every other path, it will respond with a 404 Not Found.
At this point, your \helloNode should look like this:
Now, it's time to see our lil monster works! In Command Prompt type: node index.js
Go to: http://localhost:3000/
If you are able to see the "Hello World" message is because everything went as planned. If not, you might be missing something.
You can see and download the code for this project from my git account:
https://github.com/rolando-febrero/helloNode
https://github.com/rolando-febrero/helloNode
Programming thought of the day:
- Hey! It compiles! Ship it!
No comments:
Post a Comment