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

p4a

v0.0.0-alpha-6a

Published

It is a simple nodejs server integrated with body, cookie parsing, file upload handler, cors connection etc.

Downloads

5

Readme

##p4a

It is a simple nodejs server integrated with body, cookie parsing, file upload handler, cors connection etc.

####Create simple server:

import { Server } from "p4a";

let server = new Server();
server.listen({port:8080});

Serve static files

Serves static files from the given path

server.serveStatic(__dirname+'/public');

Create Api over http

Make get and post requests

let endpoint = server.Api().endpoint();

endpoint.get('/', (req: Request, res: Response)=>{
    res.success({message: "Simple end point"}); // will send {status: true, data: {message: "Simple end point"}}
})

Access Express Request and Response

you can access express req and res too using req.req and res.res in callback

let endpoint = server.Api().endpoint();

endpoint.get('/', (req: Request, res: Response)=>{
    let expressRequest = req.req;
    res.res.json({message: "Simple end point"}); // will send {status: true, data: {message: "Simple end point"}}
})

Access requested body, cookies, form with files from the request

You can access requested body, cookies, form with files from the request without mounting any middleware

let endpoint = server.Api().endpoint();

endpoint.post('/',(req: Request, res: Response)=>{
         
    let body = req.body();                      // Requested body
    let cookie = req.cookies();                 // Requested cookies
    
    // Requested form with files
    req.form((err, fields, files) => {
     console.log("err: ", err);
     console.log("fields: ", fields);
     console.log("form: ", files);
    });
    
    console.log("body: ", body);
    console.log("cookies: ", cookie);
    
    
    res.cookie("hello", "Hello world");         // Set cookies
    res.success(true);                          // will send {status:1, data: true}
    //res.error(123, true);                     // will send {status:0, type: 123, message: true}
});

Endpoint chaining

let endpoint = server.Api().endpoint();

let college = new Endpoint();

let department = new Endpoint();
let library = new Endpoint();

college.hookup("library", library);
college.hookup("department", department);

endpoint.hookup("college", college)

Establish real time connection over socket

let socket = server.Api().socket();

socket.to('/chat', (connection: SocketConnection)=>{
    console.log("Connected");

    // Emit 'test' to the socket
    connection.socket.emit("message", "new message from p4a server over socket");

    // Handle when clients emits 'test'
    connection.socket.on("message", (d:any)=>{
        console.log(d);
    });
});

Configure filesystem

Configure where to upload the file for more information see the documentation

server.fileSystem.Config({
    uploadDir: 'upload' // File upload directory
});

Allow origins to access server

Allow all origins

server.allowOrigins("*")

Allow single origin

server.allowOrigins("http://localhost:4200")

Allow multiple origins

server.allowOrigins(["http://localhost:4200", "http://localhost:4300", "http://localhost:4400"])

Full example


import {Endpoint, SocketConnection, Response, Request, Server} from "p4a";


let server = new Server();


// Allow origin to access server
server.allowOrigins('http://localhost:4200');


// Configure file system
server.fileSystem.Config({
    uploadDir: 'upload'
});

let endpoint = server.Api().endpoint(); // Handle http requests

// Static form 
endpoint.serveStatic(__dirname+'/docs', {path:'/static'});

//http://localhost:8088/hi
endpoint.get('/hi', (req: Request, res: Response)=>{
        res.success({data: "data"});
    });

endpoint.post('/',(req: Request, res: Response)=>{

    let body = req.body();                      // Requested body
    let cookie = req.cookies();                  // Requested cookies

    // Requested form
    req.form((err, fields, files) => {
        console.log("err: ", err);
        console.log("fields: ", fields);
        console.log("form: ", files);
    });

    console.log("body: ", body);
    console.log("cookies: ", cookie);


    res.cookie("hello", "Hello world");         // Set cookies
    res.success(true);                          // will send {status:1, data: true}
    //res.error(123, true);                     // will send {status:0, type: 123, message: true}
});

let socket = server.Api().socket();     // Handle sockets

//Connect socket.io on http://localhost:8088/namespace
socket.to('/namespace', (connection: SocketConnection)=>{
    console.log("Connected");

    // Emit 'test' to the socket
    connection.socket.emit("test", "test done");

    // Handle when clients emits 'test'
    connection.socket.on("test", (d:any)=>{

        console.log(d);

        connection.socket.emit('test', d.toString()+": received")
    });
});

// start server on port 8088
server.start({port:8088});

Simple static files serving with p4a

Serve static files


import {Server} from "p4a";

let server = new Server();

// Static form 
server.serveStatic(__dirname+'/docs');

// start server on port 8088
server.start({port:8088});