-2

** i am try to count total word in PDF this is working fine when normal function but this implementation in API node js i didn't got response this is show promise is pending **

const express = require("express")
const app = express()
const fs = require("fs")
const pdf = require("pdf-parse")
let data = fs.readFileSync("pdf/book.pdf")
const port = 3001
app.use(express.json)

function countWords(str) {
    str = str.replace(/(^\s*)|(\s*$)/gi, "");
    str = str.replace(/[ ]{2,}/gi, " ");
    str = str.replace(/\n /, "\n");
    return "total number of word this book \n" + str.split(' ')
    .length;
}
const s = pdf(data).then(function(data) {
    console.log((countWords((data.text))));
});
console.log(s)
app.get("/data", async (req, resp) => {
    resp.send(s.then((result) => {
        console.log(result)
    }))
})
app.listen(port, () => {
    console.log(`express listing this port ${port}`);
})

My API not working i didn't got any response this show promise still pending

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
xyz
  • 1
  • 1

1 Answers1

0

From a quick glance: You are calling .then() on s while you should be calling it on resp

stibel
  • 11
  • 4