Skip to content

Commit

Permalink
use an express entry point server.js to enable rate limiting middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Hatch committed Mar 29, 2024
1 parent ad39dd1 commit 4a3fb10
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 25 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "stellarexplorer",
"description": "Ledger explorer for the Stellar network",
"version": "2.6.0-beta",
"version": "2.6.1",
"license": "Apache-2.0",
"author": "Chris Hatch <[email protected]>",
"homepage": "https://steexp.com",
Expand All @@ -25,9 +25,9 @@
"sideEffects": false,
"scripts": {
"build": "remix build",
"start": "remix-serve ./build/index.js",
"start": "node server.js",
"deploy": "fly deploy --remote-only",
"dev": "remix dev",
"dev": "nodemon server.js",
"jest-preview": "jest-preview",
"typecheck": "tsc",
"lint": "eslint --ignore-path .gitignore '**/*.{js,jsx,ts,tsx}'",
Expand Down Expand Up @@ -109,4 +109,4 @@
"lint-staged": {
"*.{js,jsx,ts,tsx}": "eslint --ignore-path .gitignore"
}
}
}
45 changes: 45 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

const express = require('express')
const { rateLimit } = require('express-rate-limit')
const { createRequestHandler } = require('@remix-run/express')
const serverBuild = require('./build/index')

const app = express()

// app.use(compression());

// limit the bots that are hitting this at a fast rate
const limiter = rateLimit({
windowMs: 8 * 1000,
limit: 5,
standardHeaders: 'draft-7',
legacyHeaders: false,
})
app.use('/account/*', limiter);

app.use((req, res, next) => {
const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
console.log(`request ip: ${ip}`);
next();
});

app.use(express.static("public"));


// ... set up any middleware, like rateLimit, here

app.all(
'*',
createRequestHandler({
build: serverBuild,
getLoadContext() {
// Your context here
},
})
)

const port = process.env.PORT ?? 3000

app.listen(port, () => {
console.log(`Express server listening on port ${port}`)
})
52 changes: 31 additions & 21 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
{
"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ES2019"],
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"moduleResolution": "node",
"resolveJsonModule": true,
"target": "ES2019",
"strict": true,
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"~/*": ["./app/*"]
"include": [
"remix.env.d.ts",
"**/*.ts",
"**/*.tsx",
"app/server.js"
],
"compilerOptions": {
"lib": [
"DOM",
"DOM.Iterable",
"ES2019"
],
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"moduleResolution": "node",
"resolveJsonModule": true,
"target": "ES2019",
"strict": true,
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"~/*": [
"./app/*"
]
},
// Remix takes care of building everything in `remix build`.
"noEmit": true,
"module": "commonjs", // NOTE: added to support dynamic import of p-retry (see: https://www.typescriptlang.org/ja/tsconfig#module)
},

// Remix takes care of building everything in `remix build`.
"noEmit": true,
"module": "commonjs", // NOTE: added to support dynamic import of p-retry (see: https://www.typescriptlang.org/ja/tsconfig#module)
},
}
}

0 comments on commit 4a3fb10

Please sign in to comment.