Browse Source

Merge pull request #280 from butlerx/assets-static

allow assets to be served project root rather then pwd
pull/284/head v2.0.2
Cian Butler 4 years ago
committed by GitHub
parent
commit
e82e2cb2ac
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      package.json
  2. 5
      src/server/socketServer/assets.ts
  3. 4
      src/server/socketServer/middleware.ts
  4. 12
      src/server/socketServer/shared/path.ts
  5. 2
      yarn.lock

3
package.json

@ -1,6 +1,6 @@
{
"name": "wetty",
"version": "2.0.1",
"version": "2.0.2",
"description": "WeTTY = Web + TTY. Terminal access in browser over http/https",
"homepage": "https://github.com/butlerx/wetty",
"license": "MIT",
@ -104,6 +104,7 @@
"express": "^4.17.1",
"express-winston": "^4.0.5",
"file-type": "^12.3.0",
"find-up": "^5.0.0",
"fresh": "^0.5.2",
"fs-extra": "^9.0.1",
"helmet": "^4.1.0",

5
src/server/socketServer/assets.ts

@ -1,6 +1,5 @@
import { resolve } from 'path';
import express from 'express';
import { assetsPath } from './shared/path.js';
export const trim = (str: string): string => str.replace(/\/*$/, '');
export const serveStatic = (path: string) =>
express.static(resolve(process.cwd(), 'build', path));
export const serveStatic = (path: string) => express.static(assetsPath(path));

4
src/server/socketServer/middleware.ts

@ -3,7 +3,7 @@ import etag from 'etag';
import fresh from 'fresh';
import parseUrl from 'parseurl';
import fs from 'fs';
import { join, resolve } from 'path';
import { assetsPath } from './shared/path.js';
const ONE_YEAR_MS = 60 * 60 * 24 * 365 * 1000; // 1 year
@ -43,7 +43,7 @@ export function redirect(
* @returns middleware
*/
export function favicon(basePath: string): RequestHandler {
const path = resolve(join('build', 'assets', 'favicon.ico'));
const path = assetsPath('assets', 'favicon.ico');
return (req: Request, res: Response, next: NextFunction): void => {
if (getPathName(req) !== `${basePath}/favicon.ico`) {
next();

12
src/server/socketServer/shared/path.ts

@ -0,0 +1,12 @@
import findUp from 'find-up';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
const filePath = dirname(
findUp.sync('package.json', {
cwd: dirname(fileURLToPath(import.meta.url)),
}) || process.cwd(),
);
export const assetsPath = (...args: string[]) =>
resolve(filePath, 'build', ...args);

2
yarn.lock

@ -2323,7 +2323,7 @@ find-cache-dir@^3.3.1:
make-dir "^3.0.2"
pkg-dir "^4.1.0"
find-up@5.0.0:
find-up@5.0.0, find-up@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/find-up/-/find-up-5.0.0.tgz#4c92819ecb7083561e4f4a240a86be5198f536fc"
integrity sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==

Loading…
Cancel
Save