Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lite-web-server

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lite-web-server - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2-beta.1633053420

.github/workflows/node.js.yml

13

lib/filetype.js

@@ -1,2 +0,9 @@

module.exports = function (filename) {
/**
* Get content type of the file name for http header.
* @function
* @param {string} Filename - Filename to get header content type.
* @returns {string} Filetype
*/
function GetFileType(filename) {
var filetype = "text/plain";

@@ -46,3 +53,3 @@ if (filename.endsWith(".html")||filename.endsWith(".htm")) {

} else {
filetype = "text/plain"
filetype = "text/plain";
}

@@ -52,1 +59,3 @@ return filetype;

module.exports = GetFileType;

135

lib/index.js

@@ -1,68 +0,89 @@

const http = require('http')
const http = require('http');
const path = require('path');
const fs = require("fs")
const filetype = require("./filetype")
const fs = require("fs");
const filetype = require("./filetype");
function WebServer(opts) {
this.opts = opts;
}
/**
* Start a WebServer.
* @constructor
* @param {Object} [options] - WebServer Options.
* @param {string} [options.directory=./public/] - The directory to load the file.
* @param {string} [options.notfound] - Html code to respond on not found.
* @param {Number} [options.port=3000] - Port to host the page.
* @returns {boolean}
*/
WebServer.prototype.start = async function(opts) {
var opt = this.opts;
if (!opt) {
var options = {
directory: "./public",
notfound: `<!DOCTYPE html>\n<html>\n<head>\n<title>404 Not Found</title>\n</head>\n<body>\n<center>\n<h1>404 Not Found</h1>\n</center>\n<hr>\n<center>Node.js lite-web-server</center>\n</body>\n</html>`,
port: 3000
}
} else {
var _dir = opt.dir ||opt.directory || "./public";
if (_dir == "/") {
var __dir = "."
} else if (_dir.endsWith("/")) {
var __dir = _dir.slice(0, -1);
} else {
var __dir = _dir
}
if (__dir.startsWith("/")) {
var dir = "."+__dir
} else {
var dir = __dir
}
var options = {
directory: dir || "./public",
notfound: opt.notfound || `<!DOCTYPE html>\n<html>\n<head>\n<title>404 Not Found</title>\n</head>\n<body>\n<center>\n<h1>404 Not Found</h1>\n</center>\n<hr>\n<center>Node.js lite-web-server</center>\n</body>\n</html>`,
port: opt.port || 3000
}
class WebServer {
constructor(opts) {
this.opts = opts;
}
try {
var dir = await fs.readdirSync(options.directory)
} catch (error) {
throw new Error(error);
}
try {
http.createServer(async function(req, res) {
var url = req.url
//console.log(url)
if (url == "/") {
var filedir = `${options.directory}/index.html`
/**
* @returns {Promise<Object | null>}
*/
start() {
var _this = this;
return new Promise(async function (resolve, reject) {
var opt = _this.opts;
if (!opt) {
var options = {
directory: "./public",
notfound: `<!DOCTYPE html>\n<html>\n<head>\n<title>404 Not Found</title>\n</head>\n<body>\n<center>\n<h1>404 Not Found</h1>\n</center>\n<hr>\n<center>Node.js lite-web-server</center>\n</body>\n</html>`,
port: 3000
}
} else {
var filedir = `${options.directory}${url}`
var _dir = opt.dir || opt.directory || "./public";
if (_dir == "/") {
var __dir = ".";
} else if (_dir.endsWith("/")) {
var __dir = _dir.slice(0, -1);
} else {
var __dir = _dir;
}
if (__dir.startsWith("/")) {
var dir = "." + __dir;
} else {
var dir = __dir;
}
var options = {
directory: dir || "./public",
notfound: opt.notfound || `<!DOCTYPE html>\n<html>\n<head>\n<title>404 Not Found</title>\n</head>\n<body>\n<center>\n<h1>404 Not Found</h1>\n</center>\n<hr>\n<center>Node.js lite-web-server</center>\n</body>\n</html>`,
port: opt.port || 3000
}
}
try {
var httpcontent = await filetype(filedir.toString())
var file = await fs.readFileSync(filedir)
res.writeHead(200, { "Content-Type": httpcontent })
res.end(file)
var dir = await fs.readdirSync(options.directory);
} catch (error) {
res.writeHead(404, { "Content-Type": "text/html" })
res.end(options.notfound)
//console.log(error)
reject(new Error(`Please create directory "${options.directory}" first.`));
return;
}
//fs.Dir.readSync(options.directory+url)
//console.log(options.directory)
}).listen(options.port);
} catch (error) {
throw new Error(error)
try {
var httpserver = http.createServer(async function (req, res) {
var url = req.url
//console.log(url)
if (url == "/") {
var filedir = `${options.directory}/index.html`
} else {
var filedir = `${options.directory}${url}`
}
try {
var httpcontent = await filetype(filedir.toString())
var file = await fs.readFileSync(filedir)
res.writeHead(200, { "Content-Type": httpcontent })
res.end(file)
} catch (error) {
res.writeHead(404, { "Content-Type": "text/html" })
res.end(options.notfound)
//console.log(error)
}
//fs.Dir.readSync(options.directory+url)
//console.log(options.directory)
}).listen(options.port);
resolve(httpserver);
} catch (error) {
reject(new Error(error));
}
});
}

@@ -69,0 +90,0 @@ }

{
"name": "lite-web-server",
"version": "1.0.1",
"version": "1.0.2-beta.1633053420",
"description": "creates simple web server.",

@@ -10,3 +10,3 @@ "keywords": [

],
"homepage": "https://github.com/chasyumen/lite-web-server",
"homepage": "https://lite-web-server.js.org/",
"bugs": {

@@ -17,4 +17,5 @@ "url": "https://github.com/chasyumen/lite-web-server/issues"

"scripts": {
"start": "node example.js",
"main": "node index.js",
"test": "node test.js"
"test": "node check.js"
},

@@ -30,4 +31,3 @@ "repository": {

"node": ">=12.0.0"
},
"engineStrict": true
}
}
# lite-web-server
[![TotalDownloads](https://img.shields.io/npm/dt/lite-web-server)](https://npmjs.com/package/lite-web-server) [![Build](https://img.shields.io/github/checks-status/chasyumen/lite-web-server/main)](https://github.com/chasyumen/lite-web-server) [![License](https://img.shields.io/npm/l/lite-web-server)](https://github.com/chasyumen/lite-web-server/blob/main/LICENSE)
The npm package to create simple web server.

@@ -7,2 +9,7 @@

[Documantation](https://lite-web-server.js.org/)
[Github Repository](https://github.com/chasyumen/lite-web-server)
## Install

@@ -35,67 +42,5 @@ ```

## API
### WebServer(options)
Constructor
#### options
##### port (optional)
Type: `Number`
Default: `3000`
```js
new WebServer({
port: 3000
})
```
##### directory (optional)
Type: `String`
Default: `./public/`
The directory to load the file.
```js
new WebServer({
dir: "./public_html/"
})
```
##### notfound (optional)
Type: `String` (html code)
Default: `404 Not Found`
```js
new WebServer({
notfound: "<center><h1>File was not found on this server.</h1></center>"
})
```
### GetContentType(name)
Function
#### name
Type: `String`
Type a filename to get http header content-type.
```js
var { GetContentType } = require("lite-web-server")
var htmlname = "example.html"
var contenttype = GetContentType(htmlname)
console.log(contenttype) // "text/html"
var pngname = "image.png"
var contenttype = GetContentType(pngname)
console.log(contenttype) // "image/png"
```
## License
© chasyumen 2021. Released under the MIT license
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc