factorize js code
factorize server code