-2

I am new in NodeJS, I have a file named "employe.html" and I want to use curl in this file, so for testing the curl I put following code but it's showing nothing.

Can I use curl in this file? If yes then where I am wrong?

Here is my current code:

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage. 
    req.write('hello');
  }
  else {
    console.log("Error "+response.statusCode)
    req.write('hellos');
  }
})
vvvvv
  • 25,404
  • 19
  • 49
  • 81
amit
  • 1
  • 1
  • 18
  • 28
  • What's your problem? can you please elaborate it more? If you getting `html from google` why you need to different logic? – narayansharma91 Apr 29 '19 at 12:12
  • @narayansharma91: actually if i using above code then existing data not showing means nothing working for me,thats why i am asking where i am wrong ? should i include jquery or javascript file ? because i am working in .html file – amit Apr 29 '19 at 12:16
  • So your this statement ` console.log(body) // Show the HTML for the Google homepage. ` is not working right? – narayansharma91 Apr 29 '19 at 12:19
  • @narayansharma91: Yes you are right , for testing i using google link, i will replace with my Api link later for fetch data – amit Apr 29 '19 at 12:22
  • Pleas add some more info. BTW to you can use spawning to do this. Take a look of this question https://stackoverflow.com/questions/37229144/streaming-response-from-child-process-spawn-curl-request spawning new thread have other benefits as well. e.g. you main thread will stay clean code splitting etc. – Azeem Aslam Apr 29 '19 at 12:23
  • @AzeemAslam: actually i just want to fetch data from Api in web so for this i want to use curl , thats it – amit Apr 29 '19 at 12:26
  • do you want to serve html file in response ? Then you need to check how to serve static files. take a look of this link to serve static files in hapi. https://github.com/hapijs/inert leme know if it resolves your problem. – Azeem Aslam Apr 29 '19 at 12:38
  • If you want to host static file leme know I will help you in adding script. That way your html file will not render in browser. BR, – Azeem Aslam Apr 29 '19 at 12:40
  • @AzeemAslam: yes i just want to get response from Api using curl – amit Apr 29 '19 at 12:53
  • Look if you just want to serve html file content to browser then you don't need curl for that. All Node HTTP frameworks support to serve static files I have shared an example kindly give it a try and if you have some query feel free to ask. :) – Azeem Aslam Apr 29 '19 at 12:55
  • @nishasehgal: I am confused a little. You are using `Node.js` code inside a .html file. How? To get a response from an API in an HTML file, you can just use jQuery AJAX. curl is generally used from command lines/shells. It is much simpler to use $.get (jQuery AJAX). – Prasad Apr 29 '19 at 13:05

1 Answers1

0

Try this to serve static html file in hapi in your case employe.html to path='<domain name | localhost>/' or root of your site. you just need to fix path to html file according to your directory structure.

const Hapi = require('hapi');
const Inert = require('inert');
const server = new Hapi.Server();

server.connection({port: 9200});


server.register(Inert, (err) => {

if (err) {
    throw err;
}

server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
        reply.file('/res/html/employe.html');
    }
});
server.route({
    path: "/res/{path*}",
    method: "GET",
    handler: {
        directory: {
            path: "./res",
            listing: false,
            index: false
        }
    }});


});
server.start();
Azeem Aslam
  • 554
  • 5
  • 19