Giter VIP home page Giter VIP logo

newer.js's Introduction

Newer.js

NPM Version NPM Downloads Install Size

Table of Content

Quick start

Create a simple web server with Newer.js

Installation

# npm
npm install --save newer.js
# yarn
yarn add newer.js

Creating a simple page

Create a file named index.mjs and insert the following code:

// Import from NewerJS
import { Server } from "newer.js";

// Creating a new server
const app = new Server();

// Handle every requests
app.middleware({
    invoke: async (ctx, next) => {
        // Set response to `Hello World` 
        ctx.response += 'Hello world';

        // Move to next middleware
        await next();
    }
});

// Listen to port 8080
app.listen(8080);

// Get the HTTP or HTTPS server
app.http;

// And some more code here...

Run the file and you should see the text Hello world in localhost:8080 and every subdomains or routes

Core Concepts

Context object

  • ctx.response: The response to the client.
  • ctx.query: Get query of current request. This field is read-only
  • ctx.body: Get body of current request. This field is read-only
  • ctx.url: Get URL of current request. This field is read-only
  • ctx.statusCode: To get or set the status code (if ctx.statusCode is not set it will return undefined)
  • ctx.writeFile(path: string): Append content of a file to the response
  • ctx.header(name: string, value?: string | number | readonly string[]): Get or set a single header
  • ctx.headers(headers?: { [name: string]: string | number | readonly string[] }): Set headers or get all headers if the argument is a falsy value
  • ctx.socket: The request socket. This field is read-only
  • ctx.method: The request method. This field is read-only
  • ctx.httpVersion: The request HTTP version. This field is read-only
  • ctx.remoteAddress: The server IPv4. This field is read-only
  • ctx.rawRequest: The raw request and response. This field is read-only:
    • ctx.rawRequest.req: The raw request
    • ctx.rawRequest.res: The raw response

Middlewares

To add middlewares to the server, use Server.middleware with an object that contains the following method:

  • invoke(ctx: Context, next: () => Promise<void>): Promise<void>: Asynchronous. This method will be called when the middleware is invoked.
    • ctx: The context object
    • next(): Call the next middleware

Example

app.middleware({
    invoke: async (ctx, next) => {
        // Add to response
        ctx.response += 'Hello world';

        // Call the next middleware
        await next();
    }
});

Route handling

Router is a middleware that handles a specific route and sub-route

To create a router middleware, use new Router() with:

  • path: string: The router base path

To handle a route, use Router.route with the following arguments:

  • routeName: string: The route name
  • routeHandler: Handler: The route handler

To register a middleware, use Router.middleware with:

  • ...m: Middleware[]: Middlewares to register

Example

// Import from NewerJS
import { Router } from "newer.js";

// Create a router
const index = new Router("/index");

index.middleware({
    invoke: async (ctx, next) => {
        // Add to every response in route
        ctx.response += "You are on path ";
        // Go to next middleware or route handler
        await next();
    }
});

// Create a handler of sub-route / -> This will be called if the request pathname is `/index`
index.route("/", {
    GET: async ctx => {
        // Write the response
        ctx.response += ctx.url;
    }
});

You can nest Routers using Router.middleware

Sub-domain handling

Handles a specific sub-domain

To create a sub-domain middleware, use new SubDomain() with:

  • domain: string: The sub-domain name

To register a middleware, use SubDomain.middleware with:

  • ...m: Middleware[]: Middlewares to register

Example

// Import from NewerJS
import { SubDomain } from "newer.js";

// Create a new subdomain handler (example `sub.example.com`)
const sub = new SubDomain("sub");

// Register a middleware
sub.middleware({
    invoke: async (ctx, next) => {
        // Add to response
        ctx.response += "Hello, you are on sub-domain 'sub'";

        // Move to next middleware
        await next();
    }
});

You can nest subdomains using SubDomain.middleware

Cookie

Get and set cookie using ctx.cookie which can be used for session management

To create a cookie middleware, use new Cookie() with:

  • options: CookieOptions: The cookie options

Example

// Import from NewerJS
import { Cookie } from "newer.js";

// Add the cookie middleware to 'app'
app.middleware(new Cookie({
    // Cookies last for 120 seconds
    maxAge: 120000
}));

// Example use
app.middleware({
    invoke: async (ctx, next) => {
        // Get cookies
        ctx.cookie;

        // Set cookies
        ctx.cookie = {
            // Properties here...
        };

        // Invoke next middleware
        await next();
    }
});

Pre-setup server

Set up a server with just 3 lines of code

Getting started

Create a file named index.mjs and write:

import { app } from "newer.js";

app.start();

Set up a project structure:

public ## Or the static directory that matches the configuration
src ## Source codes
    controllers ## App controllers
    middlewares ## App middlewares

Route handling

To add a route handler, for example /, create a file in src/controllers and write:

export default {
    // Route '/'
    "/": {
        // GET method
        GET: async ctx => {
            // Handles the route ...
        }
    }
}

Or if you want to use CommonJS, replace export default with module.exports:

module.exports = {
    // Route '/'
    "/": {
        // GET method
        GET: async ctx => {
            // Handles the route ...
        }
    }
}

Middlewares

To add a middleware, create a file in src/middlewares that export a middleware:

export default {
    invoke: async (ctx, next) => {
        // Some more code here ...
        await next();
    }
}

Or using CommonJS module:

module.exports = {
    invoke: async (ctx, next) => {
        // Some more code here ...
        await next();
    }
}

Configurations

Use app.config with an object that has these properties:

  • projectPath: string: The project root. Defaults to .
  • static: string: The default static directory. Defaults to public

Server options (httpOptions)

  • port: number: The server port. Defaults to 80
  • hostname: string: The server hostname. Defaults to localhost
  • httpsMode: boolean: Toggle HTTPS mode. Defaults to false
  • backlog: number: The server backlog. Defaults to 0
  • advanced: http.ServerOptions | https.ServerOptions: Other server options

newer.js's People

Contributors

aquapi avatar alguerocode avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.