npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

server-js

v0.1.4

Published

leight wight web server for node.js

Downloads

38

Readme

server-js

light weight web framework for node.js

NPM Version Build Status

###Documentation

installing server-js using npm


$ npm install server-js

server-js api


var server = require('server-js');

####server.use()

you can insert middleware using server.use() method

example:-

      
  server.use(morgan());
  server.use(bodyparser());

####server.static();

you can serve static contents using with 'static' method input to the static method should be a directory. you can server static content form different directories by passing different directories to server.static().

example:-


  server.static(__dirname);
  server.static(__dirname+'/test');

####server.settings

it is an object it has two propeties their default values are given below


  server.settings={
    providebody:true,
    limit:100*1024
  }

if providebody set to false 'server-js' will not parse the body. you can use any other middleware to achieve that, if providebody set to true you can set limit of request body length using settings object limit property. it defaults to 100kb.

example:-

      
  server.settings={
    providebody:true,
    limit:10*1024
  }
      

####server.verb();

supported verbs:- get,put,delete,post,trace,connect

example:-


  server.get('/path1',function(req,res){
        res.end('get path1')
  });

####server.route('path');

if you want to use different methods on single path you can do that with server.route

example: -


  server.route('path2').get(function(req,res){
             //request handling code       
  }).put(function(req,res){  
      //request handling code
  }).post(function(req,res){
      //request handling code
  }).delete(function(req,res){
      //request handling code
  });

####server.start()

returns a httpserver instance

you can you this instance when ever required with other modules like socket.io

example:-

          
  var app = require('server-js').start();
  var io = require('socket.io')(app);
  app.listen(3000,funciton(){
      console.log('Server started and listening on 3000 port');
  });

server.start().listen()

this is equivalent to http module listen


 var app = require('server-js');
  app.start().listen(3000,funciton(){
        console.log('Server started and listening on 3000 port');
  });
    

request api

req.params

This property is an object containing properties mapped to the named route "parameters". For example, if you have the route /user/:name, then the "name" property is available to you as req.params.name. This object defaults to {}.

u can define path's with parameters like the following

    
    example:-
    
    //single parameter
    
  server.get('/path/:id',function(req,res){
      res.end(req.params.id);
  });
  
  //multiple parameters
  
  server.get('/path/:id/:value',function(req,res){
      console.log(req.params.id+''+req.params.value);
      res.end('request parameters');
  });
    
    

response api

res.send()

you can send strings,objects and buffers using res.send method

it automatically sets the response content-type header based on the data passed to the method

u can use res.send method in following ways

Examples:-


	server.get('route',function(req,res){
		res.send({data:'response'}); //sending an object
		//automaticallys sets the header application/json
	});
	
	server.get('route',function(req,res){
		res.send('data'); //sending an object
		//automatically sets the header text/html
	});
	
	server.get('route',function(req,res){
		res.send(new Buffer(3)); //sending an object
		//automatically sets the header application/octet-stream
	});
	

####Sample server written using server-js


var server = require('server-js');


/*   server.use();   
  *   
*   here you can write the middleware code like checking whether the user has login etc.
*   here you can use all the middlewares supported by the express webframework
*   like for example :- 
*      
*     server.use(morgan());//it is a logging middleware
*
*   u should always write your middleware at the top request will traverse in the order
*   you written your server so your middleware should be at the top.
*   you can use more than one middleware like the following.
*
*     server.use(morgan());
*     server.use(bodyparser());
*     
*      server.use(function(req,res,next)){
*       if you are using a function as a middleware you should call next() at
*        the end of the function otherwise the request will not reach other layers
*        in your server.
*      }
*
*    example:- 
*              
*               server.use(function(req,res,next){
*                    // --middleware code--
*                     next();               
*               });
*
*    
*    if you are handled the request in middleware based on condition like this you should not call
*    next() after u handled the request,if you do an exception will be thrown.
*                
*     server.use(function(req,res,next){
*                         
*         if(somecondition){
*             res.end('Error');
*             //you should not call next() here because you already handled request
*         }else{
*             next(); 
*         }                  
*                         
*    });
*
*/

server.use(function(req,res,next){
  
  //middleware code
  
	next();
});

server.static(__dirname);

server.static(__dirname+'/test');


server.get('/path', function(req, res) {
	res.end('path get');
})

server.get('/path/:id', function(req, res) {
	res.end('path get with param' + req.params.id);
});

server.route('/route').get(function(req, res) {
	res.end('received');
}).put(function(req, res) {
	res.end('received put');
}).delete(function(req, res) {
	res.end('received delete');
});

server.route('/route2').get(function(req, res) {
	res.end('received route2 get');
}).put(function(req, res) {
	res.end('route put');
});

server.start().listen(3000, function() {
	console.log('server started');
});


License

MIT