-
-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get client ip? #230
Comments
i think client ip should be available after this PR oven-sh/bun#6165 is done |
Hi 👋 since Bun v1.0.4 you can do something like this: import { Elysia } from 'elysia';
const app = new Elysia();
app
.derive((ctx) => ({
remoteAddress: () => app.server!.requestIP(ctx.request)
}))
.get('/', (ctx) => ctx.remoteAddress())
.listen(3000);
console.log(`Listening on http://${app.server!.hostname}:${app.server!.port}`); Response: {
"address": "::1",
"family": "IPv6",
"port": 40908
} |
Is there a plan to add the IP to the context request object by default? If not, I am down to try to add it. I am relatively new here but I am sure it shouldn't be too hard. Please let me know if having it in the context request object is desired or not :) |
You can use https://github.com/gaurishhs/elysia-ip |
Thank you for providing this library, it works well on the main Elysia app instance ;-) However, if I try to use this in a plugin, I get
Is there any way to make this work in a specific plugin only ? |
that means the server isn't up yet, Try to use the plugin inside an Elysia app instance and call the .listen method, It should work well. Currently, You don't seem to call .listen |
I am calling .listen on the main Elysia app instance, here is my code:
Here,
it actually DOES work, but I doubt that's how it's meant to be used? P.S. removing the ip-related lines from the main instance does not make a difference. |
I am facing the same, i don't really know the reason behind the same. I believe its somehow related to how plugins work in Elysia |
@cassinius Hi, i seem to have found a fix for the same. Not initalizing the ip in plugin seems to work. import { Elysia } from "elysia";
import { ip } from "elysia-ip";
const plugin = new Elysia()
// .use(ip())
.get("/ip-from-plugin", ({ ip }) => ip);
const app = new Elysia()
.use(ip())
.get("/ip", ({ ip }) => ip)
.use(plugin)
.listen(3000); The autocomplete is not there for some reason in plugin and there on the app instance which doesn't really make sense to me (It's probably related to how .derive works). I reached out to salty for further help. |
// getting request ip
app.use(ip()); TypeError: undefined is not an object (evaluating 'request.headers') not working |
Can you share your complete code and how youre making the request. The
headers shouldnt be undefined though.
…On Fri, 6 Oct, 2023, 20:18 xfxpositions, ***@***.***> wrote:
// getting request ipapp.use(ip());
TypeError: undefined is not an object (evaluating 'request.headers')
not working
—
Reply to this email directly, view it on GitHub
<#230 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ASZRZB75MJIQQWDZBHUHBDLX6AK5FAVCNFSM6AAAAAA5IALIBCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJQHAYDSNZTG4>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Close as elysia-ip plugin and You can natively get IP from Elysia request like this, make sure server is running: new Elysia()
.get('/', ({ server, request }) => {
server?.requestIP(request)
})
.listen(3000) |
i'm trying to find something like this
from koajs
The text was updated successfully, but these errors were encountered: