nghost

1.1.15 • Public • Published

nghost

Install

$ npm i nghost

Description

Allows you to host web files such as the files generated by Angular from ng build --prod command.

Default content type

Name Content type Encodage type
html text/html text
javascript application/javascript text
style sheet text/css text
favicon image/x-icon binary
Javascript notation application/json text
Web Open Font Format application/font-woff woff binary
Web Open Font Format 2 application/font-woff2 binary
Embedded OpenType application/vnd.ms-fontobject eot binary
True Type application/octet-stream binary
Map application/octet-stream binary
MPEG-4 video/mp4 binary
Adobe Flash video/x-flv binary
Scalable Vector Graphics image/svg+xml binary
Jpg image/jpg binary
Jpeg image/jpeg binary
Png image/png binary

How to use

var nghost = require("nghost");

//Set Options

var options = {
  publicFolder: __dirname + "/dist",
  port: 8080,
  favicon: __dirname + "/dist/favicon.ico",
};

//Init Server

//nghost() function return Express application server.

var app = nghost(options);

//It's Done, Have a fun

Options

Here is all options params.

var  options = {
	publicFolder: path.join(__dirname, 'public'),
	hostname: 'localhost',
	favicon: path.join(__dirname, 'favicon.ico'),
	port: 80,
	https: false,
	key: path.join(__dirname, 'security', 'server.key'),
	cert: path.join(__dirname, 'security', 'server.crt'),
	ca: path.join(__dirname, "security", "chain.pem"),
	bodyParser: true,
	encode: 'utf-8',
	session: '',
	contents: [
		...
		//all content filter
		...
	],
	gets: [],
	posts: [],
	puts: [],
	deletes: [],
	"Access-Control-Allow-Origin": "*",
	"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept",
	simpleAuth: {
		enable: false,
		username: "",
		password: "",
		allowip: []
	},
	auth0: { //Auth0 login
		enable: false,
		AUTH0_DOMAIN: "",
		AUTH0_CLIENT_ID: "",
		AUTH0_CLIENT_SECRET: "",
		AUTH0_CALLBACK_URL: "",
	}
}

NgHost and Auth0

Default login system are append in project, "Auth0". For starting to use, change state of options.auth0.enable to true and complete your tenant information. If you not have, create an account on "Auth0.com"

Reserved route

  • (hostname):(port)/login
  • (hostname):(port)/logout

How it work

once logged user information stored in req.oidc.user, and auth status can check in req.oidc.isAuthenticated()

Simple Auth

Basic authorisation to acces on the web page.

...
simpleAuth: {
		enable: false,
		username: "",
		password: "",
		allowip: []
	},
...

Set enable to true and define username, password. if you want allow specific ip or network, add it in allowip array example:

	...
	allowip: [
		"192.168.1.1",
		"10.0.0.*"
	]
	...

in this example, ip 192.168.1.1 and all 10.0.0.* client can access without password.

Advance

To Add new requests, you must specify it in the Gets or Posts array in the options

//Example
//Traditionnal get and post
app.get("/foo", function(req, res){
	console.log("Your request is:", req.url);
	res.end();
})

app.post("/foo", function(req, res){
	console.log("your post var is:", req.body);
	res.end();
})

//nghost get and post
var options = {
....
	gets:[
		{
			key: "/foo",//regex
			function:  function (req, res) {
				console.log("Your request is:", req.url);
				res.end();
			}
		}
	],
	posts:[
		{
			key: "/foo",//regex
			function:  function (req, res) {
				console.log("your post var is:", req.body);
				res.end();
			}
		}
	]
....
}

Do not forget to add req and res arguments.

To Add new content-Type

var options = {
	....
	contents: [
		{
			key: /\.html$/,
			type:  'text',
			contentType:  'text/html'
		}
	]
	....
}

Place all web file in your specified public folder.

Change Log

######v. 1.0.2

  • debug: add body-parser for getting post value
//new options
var options = {
	....
	bodyParser: true, // True or False
	....
}

######v. 1.0.3

  • add: Session management
//new options
var options = {
	....
	session: 'SESSION_SECRET_KEY',
	....
}

######v. 1.0.5

  • debug: correction Post and Get Matching

######v. 1.0.10

  • debug: correction regex matching Post and Get in query

######v. 1.0.11

  • debug: correction binary content type

######v. 1.0.12

  • add: Access-Control-Allow-Origin headers params added

######v. 1.1.0

  • add: Auth0 authentification
  • debug: options merge
  • debug: binary content-type

######v. 1.1.7

  • add: Simple authentification

######v. 1.1.10

  • add: Https protocol Change https to true and set key and cert location

######v. 1.1.13

  • add: put,delete request

######v. 1.1.15

  • add: option ca for https credential

Package Sidebar

Install

npm i nghost

Weekly Downloads

2

Version

1.1.15

License

ISC

Unpacked Size

19.8 kB

Total Files

6

Last publish

Collaborators

  • emmo30