A
A
Alisa942019-01-30 09:19:02
Node.js
Alisa94, 2019-01-30 09:19:02

How to go to the right address?

The problem is related to splitting the server.js file into modules. I am working on the autodesk.forge platform viewer and according to the documentation I created this viewer. To create a server, express and axios are used (well, other technologies, but the problem is related to the mentioned technologies). I also created a public folder, in it an html file with an authentication hyperlink.
Below is the working code.

const path = require('path');
const express = require('express');         
const Axios = require('axios');              
const bodyParser = require('body-parser');    
const querystring = require('querystring');



let app = express();
app.use(bodyParser.json());



const config = require('./config');

const PORT = config.credentials.PORT; 


app.set('port', 3000);
let server = app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });


let access_token = '';


if (process.env.FORGE_CLIENT_ID == null || process.env.FORGE_CLIENT_SECRET == null) {
    console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.');
    return;
}


app.get('/api/forge/oauth', function (req, res) {
  Axios({
    method: 'POST',
    url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    data: querystring.stringify({
      client_id: config.credentials.client_id,
      client_secret: config.credentials.client_secret,
      grant_type: 'client_credentials',
      scope: config.scopes.internal
    })
})
    .then(function (response) {
      access_token = response.data.access_token;
      console.log(response);
      res.redirect('/api/forge/datamanagement/bucket/create');
    })
    .catch(function (error) {
      console.log(error);
      res.send('Failed to authenticate');
    });
});

<body>
    <main id="main">
        <a href="/api/forge/oauth">Authorize me!</a>
    </main>
</body>

After splitting this code into a module, it stops working, here is the modified server.js
const path = require('path');
const express = require('express');        
const Axios = require('axios');            
const bodyParser = require('body-parser'); 
const querystring = require('querystring');


let app = express();
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public'))); 

const config = require('./config');

const PORT = config.credentials.PORT;


app.use('/api/forgee', require('./routes/oauth'));
app.use((err, req, res, next) => {
  console.error(err);
  res.status(err.statusCode).json(err);
});


let server = app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });

And here is its oauth.js module
const path = require('path');
const express = require('express');
const Axios = require('axios');
const querystring = require('querystring');        

const config = require('../config');

let router = express.Router();
let access_token = '';


if (process.env.FORGE_CLIENT_ID == null || process.env.FORGE_CLIENT_SECRET == null) {
    console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.');
    return;
}


router.get('/api/forge/oauth', function (req, res) {
  Axios({
    method: 'POST',
    url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    data: querystring.stringify({
      client_id: config.credentials.client_id,
      client_secret: config.credentials.client_secret,
      grant_type: 'client_credentials',
      scope: config.scopes.internal
    })
})
    .then(function (response) {
      access_token = response.data.access_token;
      console.log(response);
      res.redirect('/api/forge/datamanagement/bucket/create');
    })
    .catch(function (error) {
      console.log(error);
      res.send('Failed to authenticate');
    });
});

module.exports = router;

The code divided into modules works like this: the server starts on port 3000, the page appears as a hyperlink, after clicking on it, a page with the text "Cannot GET /api/forge/oauth" opens. I checked and made sure that the oauth.js module is included in the main file, but I don't understand why axios can't find the /api/forge/oauth page.
The server file works for me without problems, but when I split it into modules, problems appear.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2019-01-30
@Alisa94

Replace "router.get('/api/forge/oauth'.." with "router.get('/oauth'..." in oauth.js
AND app.use('/api/forgee', require(' ./routes/oauth')); to app. use('/api/forge', require('./routes/oauth'));
Otherwise, you get the URL "/api/forgee/api/forge/oauth"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question