p4a
TypeScript icon, indicating that this package has built-in type declarations

0.0.0-alpha-6a • Public • Published

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});

Package Sidebar

Install

npm i p4a

Weekly Downloads

2

Version

0.0.0-alpha-6a

License

MIT

Last publish

Collaborators

  • atik