GithubHelp home page GithubHelp logo

Comments (5)

marc2332 avatar marc2332 commented on May 22, 2024 2

Thanks ! it worked!

app.get('/redirect',(req,res)=>{
	(res as any).location("/home").sendStatus(301);
})

from opine.

asos-craigmorten avatar asos-craigmorten commented on May 22, 2024 2

Thanks ! it worked!

app.get('/redirect',(req,res)=>{
	(res as any).location("/home").sendStatus(301);
})

Released in version 0.5.1 - if you pull done the latest, you will no longer need the any type casting.

from opine.

asos-craigmorten avatar asos-craigmorten commented on May 22, 2024

Hi @marc2332 I'll take a look at that now - it is most likely a bug 😂

from opine.

asos-craigmorten avatar asos-craigmorten commented on May 22, 2024

Ah! @marc2332 so there is a small bug in the type definitions which is why your IDE is likely complaining that location does not exist (I had forgotten to uncomment since implementing the method!)

As to why (even if you wrote something like (res as any).location("/home")) this isn't redirecting, it is because res.location() just sets a Location header as per the Opine docs.

If you want to perform a redirect, you need to send a response yourself (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location), specifically a status code of 201 or 3xx depending on the reason for redirecting.

I am updating the types now and will add an example for using the res.location() method which will look something like:

const app = opine();

app.get("/home", function (req, res, next) {
  res.send("Hello Deno!");
});

app.get("/redirect", function (req, res, next) {
  res.location("/home").sendStatus(301);
});

app.listen(3000);
console.log("Opine started on port 3000");
console.log("Try opening http://localhost:3000/redirect");

I intend to implement the equivalent of the res.redirect() that Express has very soon.

from opine.

asos-craigmorten avatar asos-craigmorten commented on May 22, 2024

Done. Please refer to the updated API documentation and the location example --> https://github.com/asos-craigmorten/opine/tree/master/examples/location

from opine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.