newer.js v0.2.5
Newer.js
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 request to "/" route
app.middleware({
invoke: async (ctx, next) => {
// Add data to response
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
Newer.js Core
Context object
ctx.response
: The response to the client.ctx.query
: Get query of current request. This field is read-onlyctx.body
: Get body of current request. This field is read-onlyctx.url
: Get URL of current request. This field is read-onlyctx.statusCode
: To get or set the status code (ifctx.statusCode
is not set it will returnundefined
)ctx.writeFile(path: string)
: Append content of a file to the responsectx.header(name: string, value?: string | number | readonly string[])
: Get or set a single headerctx.headers(headers?: { [name: string]: string | number | readonly string[] })
: Set headers or get all headers if the argument is a falsy valuectx.socket
: The request socket. This field is read-onlyctx.method
: The request method. This field is read-onlyctx.httpVersion
: The request HTTP version. This field is read-onlyctx.remoteAddress
: The server IPv4. This field is read-onlyctx.rawRequest
: The raw request and response. This field is read-only:ctx.rawRequest.req
: The raw requestctx.rawRequest.res
: The raw response
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 namerouteHandler: 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 sub.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 topublic
Server options (httpOptions
)
port: number
: The server port. Defaults to 80hostname: string
: The server hostname. Defaults tolocalhost
httpsMode: boolean
: Toggle HTTPS mode. Defaults tofalse
backlog: number
: The server backlog. Defaults to0
advanced: http.ServerOptions | https.ServerOptions
: Other server options
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago