Showing posts with label nodejs. Show all posts
Showing posts with label nodejs. Show all posts

Process manager for Node.js apps

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
http://ift.tt/ZYh2lk

Publish your npm right

Introduction


I've been working hard lately to publish my last works. For some time I was working on an evaluation platform for IT recruiters. The idea was to be able to create quizzes and auto-evaluate them. At the beginning I was thinking of a full website to generate and evaluate. But with the time clock going on, I decided to focus on the evaluation engine.

To more easily integrate it on my other projects, I decided to make it an express middleware and to share it as an npm dependency as of the node philosophy is do one thing but do it right.

How to


To allow users to access your package easily  and to avoid breaking things, you should create a tag version for your package according to your packages.json :


git tag -a v0.0.1 
git push --tags

Actually it was really easy to publish it. First of all, you'll need an npm account by typing the following commands :

npm set init.author.name "Your Name"
npm set init.author.email "you@example.com"
npm set init.author.url "http://yourblog.com"
 
npm adduser

After that you'll only have to publish it on npm :

npm publish ./ --tag v0.0.1

And that's it, your package is online with the specified tag installed by default.

Conclusion


Remember to carefully manage your tags to avoid breaking your users applications and try to use continuous integration to maintain more easily your project.

Go have a look at mine : https://www.npmjs.com/package/hupothesis

Have fun

Cross Domain AngularJS + NodeJS

Introduction

Last time I was working on a search engine web based application using AngularJS and NodeJS. I wanted to add some data using a Node webservice running on port 8080 of my virtual machine. So I made my post request as usual using $http module but soon I discovered that changing the server port was causing a Cross Domain request. So in this article I'll describe how I resolved the problem so my server accepts the request.

How-to

First of all you need to know that when setting specific headers in a Cross Domain Request, the browser will make an OPTIONS request first to check if he can actually make the POST request.
Knowing that we first need to accept the request on server side by returning Allow Origin specific headers :

Access-Control-Allow-Origin: "*"
Access-Control-Allow-Methods: "GET,HEAD,PUT,PATCH,POST,DELETE"
Access-Control-Allow-Credentials: "true"
Access-Control-Allow-Headers: "content-type"


By answering with these headers, you say that you accepted the Cross Domain request. You can also specify a specific server origin if you need to limit the API accessibility.

As said before, this is not enough, because the browser generates an OPTIONS request, your controller will be called twice on ExpressJS side. This is why you should add a middleware to catch the OPTIONS request and return a 204 no content response.

After that, you'll have a fully compliant Cross Domain Request system in your ExpressJS.

Conclusion

As you know, you're rarely the first to face the problem, this is why I prefer not to recreate the wheel and reuse good pieces of software. In this case, you can use the cors npm, allowing you to easily integrate the Cross Domain Request. You can find it at https://github.com/expressjs/cors and it is easy as abc to use.

Hope it helps, and have fun.

How to make your DHT Crawler in 3 steps

How to make your DHT Crawler in 3 steps

Why ?


You all know that torrents indexers and trackers are the target of a witch hunting by the Copyright owners because they've decided to declare the war to free and open file sharing of content.

Lastly thepiratebay founders were the target of such a fierceness and paid it the full price with enormous fines and sentences. But the community reacted to this by releasing a database dump of the website and a project to create your own indexer, openbay. Guys from isohunt were behind this release. After that the pirate bay reopened for a time then went down and up regularly.

All this, to say that torrent repositories became nowadays something governments wants to censure. So,  I've decided to create a DHT crawler using nodeJS so everyone could install his own pirate bay that (should) be easy to install on basic hardware.

How ?


I wanted to make a project that rely on only one technology to limit dependencies, so I chose nodeJS as a base. Through npm it's so easy to install extra modules and offers all the third party necessary to our project.

So the idea is to use DHT decentralized protocol to listen to torrents downloaded by other users in order to index them in our database. Then we want to search for a movie or a book in our database.

The project is composed of 3 modules :
  1. crawlDHT : this module listen on the DHT network for torrent hashes and store them on a redis queue called DHTS. It also save DHT routing table every 10 minutes as recomended by BEP specifications. It uses the dht.js module an implementation of DHT protocol.
  2. loadDHT : this one is responsible to load hashes from redis and try to download the corresponding torrent metadata using the DHT network or directly from torcache or torrage. A specificity of the last ones is that torrent are returned as gzip files but thanks to aria2 options it is very easy to save it directly decompressed. Thanks to node-aria2, we're able to communicate with aria2 using rpc calls.
  3. loadTorrent : the last module just wait for downloaded torrent metadata to parse it and index it in our mongodb dhtcrawler database. The read-torrent module is in this case intensively used for torrent parsing.

Thanks to our queue system and mongo dynamic data format, you can easily use only one specific module or even add new features to the one you want.

The portal folder contains the website contents and is based on an express instance. The front part is done using angular library and can be accessed in the public folder.

Help ?


Yes, the project is at its very beginning, but I hope that you'll fork it and make pull requests with your improvements to share it with everyone. For example, it would be great to have the peer and seed number.

So if you want to give it a try, go check : https://github.com/FlyersWeb/dht-bay

And remember to have fun :)

Post Script


After 3 days working, my little hackberry with a limited bandwith indexed almost 1000 torrents, would be great to have an idea of what are the most shared things on DHT networks.

For those who likes to watch movies an rainy sundays, you can use peerflix to stream a torrent download to your vlc or any other player. Have a look.

NodeJS edged with C#

NodeJS edged with C#

Hi everyone, it's been a while I didn't post on my blog mainly because I didn't had the time for mainly because I was coding some new stuff and working on a project of mine you'll know about really soon.

But enough of my life, today I wanted to show you some really cool piece of code, called edgejs ! There is some time that I'm seeing Microsoft making real efforts to participate to opensource community projects (some would say for their personal interests but never mind), and I personally think it is great to have major IT company to make that effort. Software development is about sharing knowledge and making great stuff that anybody can have access to.

That's why I've been making some experiments with really interesting projects like typescript, nancyfx or mono. These projects are pushed by opensource community and take advantage of Microsoft solid engineering projects to improve them. It's an interesting step toward open community or full disclosure as called in the IT security world.

Edge.JS


But enough of these thoughts for now, today I want to introduce a really interesting project created by  Tomasz Janczuk and called Edge.js and defined as :
"The Edge.js project allows you to use .NET Framework inside of a Node.js application."
This magical project allows you to call C# methods from NodeJS or NodeJS functions from a C# project. Why the hell would you want to do that you would say ? To find why it is very interesting, you have to go to the definition of both language/framework. NodeJS work in an asynchronous single threaded environment, but CLR allows you to work in a multi threaded environment. You have the best of both worlds ! For web development native asynchronous environment are really useful but for heavy computation you would rather prefer to use all available cores or even GPU capabilities !

Proof of Concept


So how it works ? You should have a look at the documentation explaining it very well and/or Tomasz blog for some extra stuff. In this post I will only explain how to create your mono DLL and call it from your NodeJS router.

First of all we'll create our mono library project to generate the DLL. You can imagine a prime number computation using multi-threaded algorithm but to make it simple we'll only create a function that takes an integer and sum it with himself :

using System;
using System.Threading.Tasks;

namespace PrimeNumber
{
    public class Startup
    {
        public async Task<object> Invoke(object input)
        {
            int v = (int)input;
            return Helper.Add (v);
        }
    }

    static class Helper
    {
        public static int Add (int v)
        {
            return v + v;
        }
    }
}


As you can see we have a Helper::Add method in charge of this. The other method should, by convention, be named Startup::Invoke. We define it as async so the call will be non-blocking in NodeJS side. Actually we also need to use await operator to have a really asynchronous call but because this is an introduction we do not integrate it but you can refer to this post for more details.

We compile our DLL to PrimeNumber.dll and copy it to our NodeJS project in assets directory and see what's next in node side.

On NodeJS side we have a server.js file that looks like this :

var edge = require('edge');

var clrMethod = edge.func('assets/PrimeNumber.dll');

clrMethod(123, function(error, result){
  if(error) throw error;
  console.log(result);
});

As you can see it is pretty easy to use. By default the method Startup::Invoke is called in the same namespace as filename PrimeNumber but you can change almost all these options (see edge documentation)

Conclusion


As you can see this is pretty impressive. The project is still in development but open a lot of possibilities for web developers as us. I hope that this quick tour helped you.

See you, Flyers.

Most seen