2

I am integrating Forge Api in my NodeJs application. All other Api works e.g authentication , creating bucket and translation a model.When I redirect to the html page to view my model it gives the 404 error..I have been stuck on this.

Below is the code and images attached.Authentication and bucket creation Api's skipped in the below code..

Folder structure is

->server

-->app.js

-->viewer.html

enter image description here

App.js

app.post('/api/forge/datamanagement/bucket/upload/uploads/:filename', function (req, res) {


 console.log(req.params.filename);
 console.log("Token",access_token)
 var fs = require('fs'); // Node.js File system for reading files
 fs.readFile(`./uploads/${req.params.filename}`, function (err, filecontent) {
 console.log(filecontent)
   Axios({
        method: 'PUT',
        url: 'https://developer.api.autodesk.com/oss/v2/buckets/' + encodeURIComponent(bucketKey) + '/objects/' + encodeURIComponent(req.params.filename),
 headers: {
     Authorization: 'Bearer ' + access_token,
     'Content-Disposition': req.params.filename,
     'Content-Length': filecontent.length
 },
 data: filecontent
 }).then(function (response) {
     // Success
     console.log(response);
     console.log("IdUrn ====>>>>>>>"+ response.data.objectId)
     var urn = response.data.objectId.toBase64();
     console.log("In Response")
     res.redirect('/api/forge/modelderivative/' + urn);
 }).catch(function (error) {
     // Failed
     console.log(error.message);
     
     res.send(error.message);
 });
});
});

  app.get('/api/forge/modelderivative/:urn', function (req, res) {

    console.log("urrrr",req.params.urn)
    var urn = req.params.urn;
    var format_type = 'svf';
    var format_views = ['2d', '3d'];
      Axios({
          method: 'POST',
          url: 'https://developer.api.autodesk.com/modelderivative/v2/designdata/job',
          headers: {
                   'content-type': 'application/json',
                    Authorization: 'Bearer ' + access_token
                  },
          data: JSON.stringify({
            'input': {
                 'urn': urn
                   },
              'output': {
                 'formats': [
                 {
                   'type': format_type,
                   'views': format_views
                 }
                 ]
                }
                })
      })
         .then(function (response) {
      // Success
      console.log("Translated=============>>>",response);
      res.redirect('/viewer.html?urn=' + urn);
  })
  .catch(function (error) {
      // Failed
      console.log(error);
      
      // res.send(error.message);
  });
  });

Response in the network Tab

enter image description here

Shehryar Tanoli
  • 393
  • 2
  • 4
  • 17
  • 1
    You should place the ```viewer.html``` file in your public folder and serve this folder as static in your NodeJS app (I supposed it's app.js) with something like this ```app.use(express.static('public'));``` . This will allow you to reach the page ```http://localhost:3000/viewer.html```. Try to go to this page without the query to ensure you can access this page. – AlexAR Aug 30 '21 at 14:35
  • @AlexAR In the response tab i get the desired html page,as my request is going through react frontend which is localhost:3000 and server is on localhost:8080.I want the html response to be opened in the new tab when request is made to the that API.How can I open the static file which is on my server in the new tab? – Shehryar Tanoli Sep 01 '21 at 08:24
  • 1
    I don't understand why you're dealing with html files if you use React. Usually your client make a request to your API to translate your file after upload. As it take some time to do the translation, if you don't have webhooks, you will need to refresh the page and ask for the manifest to see if the translation is done (or eventually you can do a loop to check that and start the viewer when it's done). The response of the job don't tell you that the file is translated, but that the job is accepted or not. You need to check the manifest to get the translation status. – AlexAR Sep 01 '21 at 12:10

0 Answers0