Node.js == serverside Javascript
A change is gonna come, that’s what Sam Cooke sang in 1964. I don’t know if he could have foreseen the major shifts in technology in our time, but it’s evidently that JavaScript has become increasingly popular as a web language. First only at the client, but nowadays at the server side too. The more surprising if you realize that it’s nibbling at Java, once the ruler of web backend applications. Well, ok, according to some people.
Also, JSON is slowly replacing XML as messaging markup protocol, especially with the growth of IoT devices and mobile, and it’s probably not a coincidence that JSON was written for JavaScript and these two are deeply integrated in to each other.
Although it’s extremely interesting how JavaScript is used in the mobile backend of Oracle Mobile Cloud Service (MCS), let’s start with the basics. How can you use JavaScript as a server-side language?
Here’s where Node.js comes in the picture. Node.js is a JavaScript Runtime built on Chrome’s V8 JavaScript engine. When you install it, you get npm as a bonus too! npm is a package manager and although there are a few too many of those already, it’s really useful when working with all types of JavaScript libraries and you want to reuse and share your code.
Let’s take a look at how Node.js works (this is an excerpt from the documentation).
The core API supports an event-driven architecture in which objects (called “emitters”) periodically emit named events that cause Function objects (“listeners”) to be called.
There are, for instance, events emitted each time a peer connects to it, when a file is opened, or whenever data is available to be read.
One class is extremely important and that is the EventEmitter
class. All the objects that emit events are instances of this class. They expose an eventEmitter.on()
function that allows one or more Functions to be attached to named events emitted by the object.
When the EventEmitter
object emits an event, all of the Functions attached to that specific event are called synchronously.
Another important concept is the stream. It’s an abstract interface, which can be implemented by for example a request to an HTTP server. Streams are readable, writable, or both (duplex).
All streams are EventEmitters, but they also have other custom methods and properties depending on whether they are Readable, Writable, or Duplex.
If a stream is both Readable and Writable, then it implements all of the methods and events. So, a Duplex or Transform stream is fully described by this API, though their implementation may be somewhat different.
Although many things can be told about how to leverage node.js for your website, I’ll conclude this post with a simple “Hello World” example how to create your first node.js backend code.
Create a file called “MyFirstNode.js”, and add the following code:
const http = require('http');
http.createServer( (request, response) => {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello Worldn');
}).listen(8124);
console.log('Node.js server now running at http://127.0.0.1:8124/');
Executing the following command starts the server:
$ node MyFirstNode.js
Server running at http://127.0.0.1:8124/
Testing the link in the browser results in a predictable outcome:
That’s all. Next: setting up your environment and integrate node.js in your existing infrastructure.
Overzicht blogs
Geen reacties
Geef jouw mening
Reactie plaatsenReactie toevoegen