无法使用Node JS从URL接收Jason数据

时间:2018-11-22 23:08:49

标签: javascript node.js

我正在从教程中为node.js编写基本代码,它应该从url位置:https://jsonplaceholder.typicode.com/posts上的文件中获取json数据。并将其保存在使用相同代码创建的新文件中。 我的代码在下面

const https=require('https');
const fs=require('fs');
const url='https://jsonplaceholder.typicode.com/posts';
https.get('url', res => {

    res.setEncoding('utf8');

    let body='';

    res.on('data', data => {
       body += data;

    });
   res.on('end', ()=>{
  fs.writeFile('data.json', body, 'utf8', (err)=>{
      if(err) return err;
      consle.log('data was saved');
  });
   });
});

It give me this error 

https.js:235     引发新错误.Error('ERR_INVALID_DOMAIN_NAME');     ^

Error [ERR_INVALID_DOMAIN_NAME]: Unable to determine the domain name
at request (https.js:235:13)
at Object.get (https.js:249:15)
at Object.<anonymous> (C:\Projects\Node\http-request.js:4:7)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)

谁能告诉我我错了

1 个答案:

答案 0 :(得分:0)

https.get(url, res => {

代替

https.get('url', res => {