Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

Why does this app.js file not "see" my route?

I want to utilize some "best practices" as I build the API you see below, as far as using routes, models and controllers etc.

The code below is something I'm using from "fitbit-node." It works just fine, however. I want to take some of what resonates as that which belongs in a "routes" directory and move it so, when I'm all finished, I can upload it as something that looks and feels like a complete and accurate app.

At least, that's what I'm thinking.

Here's my code:

// initialize the express application
const express = require("express");
const app = express();
const path = require("path");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
[b]//const authorizeRoutes = require("./routes/authorize");[/b]

app.use(bodyParser.json());
[b]//app.use("/authorize", authorizeRoutes);[/b]

// initialize the Fitbit API client
const FitbitApiClient = require("fitbit-node");
const client = new FitbitApiClient({
	clientId: "CLIENT_ID", //client_id
	clientSecret: "SECRET", //client secret
	apiVersion: '1.2' // 1.2 is the default
});

// redirect the user to the Fitbit authorization page
app.get("/authorize", (req, res) => {
	// request access to the user's activity, heartrate, location, nutrion, profile, settings, sleep, social, and weight scopes
	res.redirect(client.getAuthorizeUrl('activity heartrate location nutrition profile settings sleep social weight', 'http://localhost:3000/callback')); //callback url
});

// handle the callback from the Fitbit authorization flow
app.get("/callback", (req, res) => {
	// exchange the authorization code we just received for an access token
	client.getAccessToken(req.query.code, 'CALLBACK_URL').then(result => { //callback url
		// use the access token to fetch the user's profile information
		client.get("/profile.json", result.access_token)
		.then(results => {
			res.send(results[0]);
			console.log(results[0].user.age);
		}).catch(err => {
			res.status(err.status).send(err);
		});
	}).catch(err => {
		res.status(err.status).send(err);
	});
});

mongoose
    .connect(
        "DATABASE INFO"
    )
    .then(result => {
        app.listen(3000);
    })
    .catch(err => console.log(err));

Open in new window


You see the commented lines I have in bold? That's my first step in trying to break up the above code into what I'm thinking SHOULD be a collection of references to various routes.

When I un-comment the emboldened lines and use this as my "authorize" route...

const express = require("express");
const app = express();

const router = express.Router();

// initialize the Fitbit API client
const FitbitApiClient = require("fitbit-node");
const client = new FitbitApiClient({
	clientId: "22BD5D", //client_id
	clientSecret: "9677a6cdf63836e26d0617e9ac2c38f8", //client secret
	apiVersion: '1.2' // 1.2 is the default
});

// redirect the user to the Fitbit authorization page
app.get("/authorize", (req, res) => {
	// request access to the user's activity, heartrate, location, nutrion, profile, settings, sleep, social, and weight scopes
	res.redirect(client.getAuthorizeUrl('activity heartrate location nutrition profile settings sleep social weight', 'http://localhost:3000/callback')); //callback url
});

module.exports = router;

Open in new window


I get a message that says, "Cannot get /authorize."

Why?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Alright, Chris!

Once again, we'll pop the hood on what you're saying here and see what I can come up with.

I'm going to close this question, but I've encountered more snags on a previous question that you weighed in on. Take a look at https://www.experts-exchange.com/questions/29163210/I-don't-understand-how-to-access-this-endpoint.html#questionAdd
Thanks, Chris!