@nattyjs/express 0.0.1-beta.49 → 0.0.1-beta.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const express = require('express');
4
+ const path = require('node:path');
4
5
  const cors = require('cors');
5
6
  const compression = require('compression');
6
7
  const core = require('@nattyjs/core');
@@ -9,6 +10,7 @@ const common = require('@nattyjs/common');
9
10
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
10
11
 
11
12
  const express__default = /*#__PURE__*/_interopDefaultCompat(express);
13
+ const path__default = /*#__PURE__*/_interopDefaultCompat(path);
12
14
  const cors__default = /*#__PURE__*/_interopDefaultCompat(cors);
13
15
  const compression__default = /*#__PURE__*/_interopDefaultCompat(compression);
14
16
 
@@ -83,11 +85,36 @@ function requestHandler(config) {
83
85
  const app = express__default();
84
86
  const ExpressModule = {
85
87
  init(config) {
88
+ const apiPrefix = `/${common.commonContainer.nattyConfig.api?.rootPath}`;
89
+ const staticCfg = config.static;
86
90
  app.use(compression__default());
87
91
  if (config.cors)
88
92
  app.use(cors__default(config.cors));
89
93
  app.use(express__default.json({ limit: config.payload?.limit || "1mb" }));
90
- app.all("*", requestHandler(config));
94
+ if (staticCfg?.dir) {
95
+ const staticDir = path__default.resolve(staticCfg.dir);
96
+ app.use(
97
+ express__default.static(staticDir, {
98
+ index: false,
99
+ // we'll control SPA index fallback manually
100
+ maxAge: staticCfg.maxAge ?? "1d"
101
+ })
102
+ );
103
+ if (staticCfg.spaFallback !== false) {
104
+ app.get("*", (req, res, next) => {
105
+ const url = req.originalUrl || req.url;
106
+ if (url.startsWith(apiPrefix))
107
+ return next();
108
+ if (req.method !== "GET")
109
+ return next();
110
+ const acceptsHtml = req.headers.accept?.includes("text/html");
111
+ if (!acceptsHtml)
112
+ return next();
113
+ return res.sendFile(path__default.join(staticDir, staticCfg.indexFile ?? "index.html"));
114
+ });
115
+ }
116
+ }
117
+ app.use(apiPrefix, requestHandler(config));
91
118
  if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
92
119
  common.getPort().then((port) => {
93
120
  app.listen(port, () => {
package/dist/index.mjs CHANGED
@@ -1,8 +1,9 @@
1
1
  import express from 'express';
2
+ import path from 'node:path';
2
3
  import cors from 'cors';
3
4
  import compression from 'compression';
4
5
  import { HttpHandler, HttpContext } from '@nattyjs/core';
5
- import { GET, FrameworkType, getPort } from '@nattyjs/common';
6
+ import { GET, FrameworkType, commonContainer, getPort } from '@nattyjs/common';
6
7
 
7
8
  async function getRequestBodyInfo(request) {
8
9
  const contentType = request.headers["content-type"];
@@ -75,11 +76,36 @@ function requestHandler(config) {
75
76
  const app = express();
76
77
  const ExpressModule = {
77
78
  init(config) {
79
+ const apiPrefix = `/${commonContainer.nattyConfig.api?.rootPath}`;
80
+ const staticCfg = config.static;
78
81
  app.use(compression());
79
82
  if (config.cors)
80
83
  app.use(cors(config.cors));
81
84
  app.use(express.json({ limit: config.payload?.limit || "1mb" }));
82
- app.all("*", requestHandler(config));
85
+ if (staticCfg?.dir) {
86
+ const staticDir = path.resolve(staticCfg.dir);
87
+ app.use(
88
+ express.static(staticDir, {
89
+ index: false,
90
+ // we'll control SPA index fallback manually
91
+ maxAge: staticCfg.maxAge ?? "1d"
92
+ })
93
+ );
94
+ if (staticCfg.spaFallback !== false) {
95
+ app.get("*", (req, res, next) => {
96
+ const url = req.originalUrl || req.url;
97
+ if (url.startsWith(apiPrefix))
98
+ return next();
99
+ if (req.method !== "GET")
100
+ return next();
101
+ const acceptsHtml = req.headers.accept?.includes("text/html");
102
+ if (!acceptsHtml)
103
+ return next();
104
+ return res.sendFile(path.join(staticDir, staticCfg.indexFile ?? "index.html"));
105
+ });
106
+ }
107
+ }
108
+ app.use(apiPrefix, requestHandler(config));
83
109
  if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
84
110
  getPort().then((port) => {
85
111
  app.listen(port, () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nattyjs/express",
3
- "version": "0.0.1-beta.49",
3
+ "version": "0.0.1-beta.50",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "ajayojha <ojhaajay@outlook.com>",
@@ -19,9 +19,9 @@
19
19
  "chokidar": "4.0.3",
20
20
  "cors": "2.8.5",
21
21
  "compression": "1.7.4",
22
- "@nattyjs/core": "0.0.1-beta.49",
23
- "@nattyjs/common": "0.0.1-beta.49",
24
- "@nattyjs/types": "0.0.1-beta.49"
22
+ "@nattyjs/core": "0.0.1-beta.50",
23
+ "@nattyjs/common": "0.0.1-beta.50",
24
+ "@nattyjs/types": "0.0.1-beta.50"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/node": "20.3.1",