Bummer! You have been redirected as the page you requested could not be found.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to redirect in node.js without express? (In callback)

How do you redirect to a new page in node.js without express? I want to do it after some routing has been done, so a 'blocking' redirect, if you like. Here's a snippet to help you understand

function finishAuthRoute(request, response) {
    if(request.url === "/finish_auth"){
        var Shopify = new shopifyAPI({
                shop: storeType + ".myshopify.com", // MYSHOP.myshopify.com
                shopify_api_key: 'mykeyishere', // Your API key
                shopify_shared_secret: 'mysecretishere', // Your Shared Secret
                shopify_scope: 'read_content, write_content, read_customers, write_customers, read_orders, write_orders',
                redirect_uri: redirect_uri_type
            }); 
            // Exchange temporary token for permanent one   
        var query_params = request.query;
        Shopify.exchange_temporary_token(query_params, function(err, data) {
            console.log('The permanent key is: ' + data['access_token']);
        });
        // Redirect back to app (this doesn't work)
        response.writeHead(302, {Location: storeType + ".myshopify.com/admin/apps"});
        response.end(console.log('Redirecting to Shopify Admin') + '\n');
    }
}

So where it says redirect back to app, in comments - I want it to redirect back after it's processed the above code. Any ideas?

If I could put response.writeHead(302, {Location: storeType + ".myshopify.com/admin/apps"}); inside of response.timeOut maybe I'd have luck...

Cheers in advance

2 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher
var http = require("http");

http.createServer(function(request, response){
    response.writeHead(302,  {Location: "http://teamtreehouse.com"})
    response.end();
}).listen(3030);

This is the simplest code that works...

Is storeType doing a full url with http:// or https://?

storeType doesn't have any prefix, so I've since added that in! I ended up converting my node.js code to express and using .redirect(); which works like a charm!

At the time I was still confused whether response.writeHead(302, {Location: "http://teamtreehouse.com"}) would execute before the rest of the code, or after it had been completed - so I thought I needed to somehow put that code in a timeout function!

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

If something has a callback it's an asynchronous function and you can write your response in it.

If there's not option for a callback, the code is synchronous and sequential.

I might be wrong but I think Location needs to be in ' '?

Here is the thread I was using for reference

It doesn't make a difference - although I have added it in, nothing changes! :-(

Shoot, was hoping that was it. Is there a reason the + '/n' is out side of your console.log() block?

my understanding, and I could be wrong, is that you need '\n' at the end of a response.somethingHere to return it

Edit: this isn't correct '\n' puts it on a new line in the log/file

I might be wrong because I rarely use node without express but I don't think you need it. It just means "new line" I believe