Skip to content

Commit

Permalink
extension api and functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowwzw committed Mar 31, 2017
1 parent 98ae02c commit 4aa7a51
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lite-dev-server",
"version": "1.8.40",
"version": "2.0.0",
"description": "This is http file server for develpment. This server supports livereload function and proxy function for your api server.",
"main": "index.js",
"scripts": {
Expand Down
16 changes: 12 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ const fs = require("fs");
const fsp = require("fs-promise");
const mime = require('mime-types');

export const isFile = async path => {
const stats = await fsp.stat(path);
if(!stats.isFile()) return new Promise.reject(Error('this is not file'));
};

export const isDirectory = async path => {
const stats = await fsp.stat(path);
if(!stats.isDirectory()) return new Promise.reject(new Error('this is not directory'));
};

export const giveHtmlFile = async (res, path, injectStream) => {
await fsp.access(path, fs.constants.R_OK);
const stats = await fsp.stat(path);
if(!stats.isFile()) throw new Error('this is folder');
await isFile(path);
res.setHeader('Content-Type', 'text/html');
fs.createReadStream(path).pipe(injectStream).pipe(res);
};

export const giveFile = async (res, path, ext) => {
await fsp.access(path, fs.constants.R_OK);
const stats = await fsp.stat(path);
if(!stats.isFile()) throw new Error('this is folder');
await isFile(path);
res.setHeader('Content-Type', mime.contentType(ext));
fs.createReadStream(path).pipe(res);
};
48 changes: 37 additions & 11 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ const Transform = require("stream").Transform;
const chalk = require('chalk');
const MSG404 = "not found";
const CODE404 = 404;
const DEFAULT_PAGE_FIRST = "index.html";
const DEFAULT_PAGE_SECOND = "index.htm";
import { giveHtmlFile, giveFile } from './helpers';
import { giveHtmlFile, giveFile, isDirectory, isFile } from './helpers';

if (!fs.constants) {
fs.constants = {
Expand All @@ -27,6 +25,9 @@ const liteDevServer = ({
liveReloadDelay = 0,
historyApiFallback = false,
reloadDelayOnClient = 100,
giveDefaultPage = true,
defaultPageFirst = "index.html",
defaultPageSecond = "index.htm",
}) => {
const clientScript = fs.readFileSync(`${__dirname}/client.js`, 'utf8').replace(/webSocketPort/g, webSocketPort).replace(/reloadDelay/g, reloadDelayOnClient);
const _transform = function (chunk, enc, cb) {
Expand All @@ -41,7 +42,7 @@ const liteDevServer = ({
const EventEmitter = require("events");
const liveReloadEM = new EventEmitter();
const ws = require("ws");
wss = new ws.Server({port: webSocketPort});
wss = new ws.Server({ port: webSocketPort });
wss.on("connection", connection => {
console.log("\nlite-dev-server: The WebSocket connection is established successfully");
const reloadHandler = () => {
Expand All @@ -63,7 +64,7 @@ const liteDevServer = ({
});
console.log(chalk.green(`\nwatchFolders ${watchFolders}`));
watchFolders.forEach(folder => {
fs.watch(`${folder}`, {recursive: true}, () => {
fs.watch(`${folder}`, { recursive: true }, () => {
setTimeout(function () {
liveReloadEM.emit("reload");
}, liveReloadDelay);
Expand Down Expand Up @@ -106,10 +107,10 @@ const liteDevServer = ({
injectStream._transform = _transform;
if (req.url === "/" || (historyApiFallback && !ext)) {
try {
await giveHtmlFile(res, `${folder}/${DEFAULT_PAGE_FIRST}`, injectStream);
await giveHtmlFile(res, `${folder}/${defaultPageFirst}`, injectStream);
} catch (err) {
try {
await giveHtmlFile(res, `${folder}/${DEFAULT_PAGE_SECOND}`, injectStream);
await giveHtmlFile(res, `${folder}/${defaultPageSecond}`, injectStream);
} catch (err) {
console.log(chalk.red(err + ""));
res.statusCode = CODE404;
Expand All @@ -119,10 +120,35 @@ const liteDevServer = ({
}
} else {
try {
if (ext === ".html" || ext === ".htm") {
await giveHtmlFile(res, `${folder}${req.url}`, injectStream);
} else {
await giveFile(res, `${folder}${req.url}`, ext);
try {
await isFile(`${folder}${req.url}`);
if (ext === ".html" || ext === ".htm") {
await giveHtmlFile(res, `${folder}${req.url}`, injectStream);
} else {
await giveFile(res, `${folder}${req.url}`, ext);
}
} catch (err) {
try{
if(giveDefaultPage){
await isDirectory(`${folder}${req.url}`);
try {
await giveHtmlFile(res, `${folder}${req.url}${defaultPageFirst}`, injectStream);
} catch (err) {
try {
await giveHtmlFile(res, `${folder}${req.url}${defaultPageSecond}`, injectStream);
} catch (err) {
console.log(chalk.red(err + ""));
res.statusCode = CODE404;
if (page404) await giveHtmlFile(res, `${folder}/${page404}`, injectStream);
else res.end(MSG404);
}
}
} else {
throw new Error("giveDefaultPage is false");
}
} catch (err) {
console.log(chalk.red(err + ""));
}
}
} catch (err) {
console.log(chalk.red(err + ""));
Expand Down
10 changes: 10 additions & 0 deletions tests/static/folder2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
index.html in folder2
</body>
</html>
5 changes: 5 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ ava(async t => {
}
});

ava(async t => {
const result = await rp(`${server1Host}folder2/`);
t.true(result.includes("index.html in folder2"), 'get default page from folder2');
});

// server2Host

ava(async t => {
Expand Down

0 comments on commit 4aa7a51

Please sign in to comment.