简体   繁体   中英

Calling a file from local machine as a web service Javascript

I am working on a project. Where i need to call a web service which returns a SOAP message. Now the problem is that the webservice is hosted on an IntraNet and i can not access it. I have been provided with a txt file that contains a sample response of the web service. I have to manipulate the reply in my code Is there anyway I can call this txt file instead of the web service and get the response and manipulate it ?

You can do that using node and Express JS. Install Node from www.nodejs.com

after installing and running node, go to your directory and install express :

npm install express

then create a file called app.js and run it with node : node app.js

app.js file:

var express = require('express')
  , app = module.exports = express();

app.get('/', function(req, res){
  res.send('<p>Static File Server for files</p>');
});


app.get('/txt/:file(*)', function(req, res, next){
  var file = req.params.file
    , path = __dirname + '/public/txt/' + file;

  res.sendFile(path);
});


  if (404 == err.status) {
    res.statusCode = 404;
    res.send('File Does Not Exist!');
  } else {
    next(err);
  }
});

if (!module.parent) {
  app.listen(8023);
  console.log('Express started on port 8023');
}

put the folder that has your txt file in /public/txt/ so public and the app.js are in the same directory. you should run the app.js with node after this is done. go to your web browser : navigate to localhost:8023/txt/yourfile.txt and if everything is working find you should be able to get the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM