In my previous post I wrote about the clean separation of concerns by using a javascript server like node.js. In the past I worked on a project where they used node.js but personally I never got involved. I wrote the backend part of the system where we used rest services that returned json as an answer for the node.js server (javascript can handle json).
So curiousity killed the cat but made man kind evolve. So I tried to set this up on my linux machine. Googled around a bit and found the answers to my questions.
Installation:
actually on linux it is pretty basic if you have git running:
My attempt under debian:
- git clone --depth 1 git://github.com/joyent/node.git
- cd node
- git checkout v0.4.11
- export JOBS=2
- mkdir ~/local
- ./configure --prefix=$HOME/local/node
- make
- make install
- echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
- echo 'export NODE_PATH=$HOME/local/node:$HOME/local/node/lib/node_modules' >> ~/.profile
- source ~/.profile
My attempt under ubuntu:
- sudo apt-get install curl
- curl -sL https://deb.nodesource.com/setup | sudo bash -
- sudo apt-get install nodejs
(The path under ubuntu will bie /usr/bin/node)
After you done litterly this (take care of the $PATH ELEMENTS. You need to replace them with the real path where you put the node server).
after this you can create a test.js in any folder you like containing:
//require is the import in java
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
//creating the http server
var server = http.createServer(function (request, response) {
//bare writing to the response like in the old days with Java.
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8090);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8090/");
After you saved the file you have to tell nodejs that he has to start this application:
- ./node test.js in your console in the node directory
- Start a browser on the desired port 8090 in my case
The result should be hello world on a snowy white page.
have fun!
Geen opmerkingen:
Een reactie posten