Fetch local files with Node.js - javascript

In a browser environment fetching a local file is quite trivial: one just need to start a server (using MAMP, XAMP, Mac's Python server, etc...), and then doing:
fetch("./foo.txt").then(etc...)
However, in Node.js this simple task has been a challenge. I've tried the same snippet using Node 18 (which comes with an experimental fetch API), but I'm always getting an invalid URL error:
TypeError: Failed to parse URL from foo.bar
[cause]: TypeError [ERR_INVALID_URL]: Invalid URL
I've tried installing node-fetch, but I'm getting the same error. I could start a local server for node like http-server, but it tells me to go to http://localhost:8080 to see the server, that is, using the browser, but the issue is that I can do that without node, using only a node build is the whole point.
My question is: is it possible to fetch a local file in a node build (Sublime Text, VS Code etc...), without using a browser? (note: I can do it with fs, but in my question I'd like to discuss fetch only)

My question is: is it possible to fetch a local file in a node build (Sublime Text, VS Code etc...), without using a browser? (note: I can do it with fs, but in my question I'd like to discuss fetch only)
The Node.js implementation of fetch does not currently support file: scheme URLs…
fetch("file:///tmp/123").then(r => r.text()).then(d => console.log(d));
reports:
Error: not implemented... yet...
…nor (it appears) does it resolve a relative path to a file: scheme URI.
You should use the fs module.

Related

Handle global variable in node.js server

I have a node.js server that reads messages from a rabbitmq server. Every message contains an url that returns a json object whit specifications to download some jsx code.
The node.js server gets the code from the urls and compiles it with webpack.
My problem is that I need to keep aware of the information of the json objects in the webpack compilation instance, because I need to print the downloaded objects in the index page.
Node Server -- Get messages --> RabbitMQ Server
RabbitMQ Server -- Return messages --> Node Server
Node Server -- Get code [from URL] --> URL service
URL service -- Return code --> Node Server
Node Server: Compile downloaded code.
I don't know if I was clear. I tried to use global variables and module.exports, but did not work. Maybe I am missing something, I am a kind of beginner in JS, node and webpack.
Could you cache those in memory, that way they would be available to access.
One of the popular module is memory-cache
Though memory caching comes in with it's own set of limitations.
Hope I understood the question correct.

browserify Error : http.createServer is not a function

I tried to browserify this node js script :
var phantom = require('phantom')
phantom.create(function(ph) {
ph.createPage(function(page) {
page.open("editor.html", function(status) {
console.log("opened diagram? ", status);
page.evaluate(function() {
return document.getElementById("GraphImage").src;
}, function(result) {
//console.log(result);
ph.exit();
});
});
});
});
So I used this command:
browserify myscript.js > bundle.js
and when I run bundle.js from an html file I get this error:
http.createServer is not a function
it seems that browserify does not support httpserver. How can I resolve this problem?
You can't run a web server from inside a web browser. There really isn't anything in the browser that could act like Node's http module. Also it doesn't make sense to run PhantomJS in a browser, because PhantomJS is a web browser.
What is the desired behavior you are trying to accomplish?
Update:
It seems like you are trying to run code intended for Node.js inside a browser instead.
The JavaScript engine inside the browser is much more restrictive than in Node.js, for example you can't access the file system from inside the browser for security reasons (or else you could read the hard drive of anyone who visited your web page).
Browserify does include some "shims" that will put small JS libraries into your code that work in the browser and match the API of Node.js, allowing some Node.js specific JS code to execute in the browser.
In your case, you are requiring Phantom, which seems to in turn require http. Accoring to the Browserify documentation, it will see require('http') and include a shim for the http module (because browser's don't provide an http module of their own).
The Phantom module then tries to call http.createServer() but accoring to the documentation for that http shim:
This module implements http.request, http.get, and most of http.ClientRequest and http.IncomingMessage in addition to http.METHODS and http.STATUS_CODES.
so http.createServer() is not supported by the shim. This also makes sense because a browser would never let you open an http server inside of itself, or else navigation to someone's web site could cause your browser to start serving content to the outside world, which obviously doesn't make sense.
In your comment:
"i want that my node js script can be executed from another JS code"
You don't specify what "other JS code" is running in. If that JS code is already running in Node, then you don't need Browserify at all. If you are trying to have a web browser start up an actual Node.js process, that isn't going to happen, again for obvious security reasons, because browsing to a web page shouldn't have the ability to run any executable on your system.
What Browserify lets you do is take code originally intended for Node.js, and run it in a browser instead, but a t runtime it is executing in the browser, not in Node.js, so you can only use JS code that works within the constraints of the browser's JS runtime.
If you are trying to execute your code in Node.js, then you need to do that by having something start the Node.js executable, either from the command line or by having another program start the process for you, but that can't be done from within a web browser. If you are trying to have users navigate to your web site and then have this code run on their machines in a browser and not in Node.js, then you need to only use modules that work in the browser.

Deploying Node JS application over a server

I have done quite a research of deploying an application over the local server that I have on my machine. Each source code for the Node JS application or the example that is available over the internet specifies to run the application from the console.
Is there any way that i can configure my MAMP server so that when i hit a URL the Node code specified is executed.
Are there any parameters to set for the same ?
I looking forward to the steps to achieve this as i was not able to found a relevant answer for the same as such.

Getting "Not allowed to load local resource" error while trying to attach a MediaSource object as the source of a HTML5 video tag

I am trying to get this example to work. It works fine when I click the link. But when I try to download the HTML file on my local machine and try the same, it is throwing this error.
Not allowed to load local resource: blob:null/6771f68d-c4b8-49a1-8352-f2c277ddfbd4
The line of code that seems to be causing the issue is this,
video.src = window.URL.createObjectURL(mediaSource);
What this line of code is doing is basically trying to set the source of the video tag media element to the MediaSource object. I have tried various permutations without much luck.
I am using Chrome Version 28.0.1500.72 m, which is the latest stable release.
I would appreciate any pointers.
As #dandavis has said, "run it from http: not file".
I'm posting this as an answer for the sake of organization.
For starters:
Running you project from http means having a http server (such as apache or a simple node http-server) and running your project via http://localhost.
Install http-server globally using npm command(provided you have installed Node.js in your system beforehand). Navigate to your file folder in CMD and type http-server. Your app should run in localhost:8080.

How to get node asset_builder working with current express?

I've been trying to use the asset_builder node module with express 3.1, but can't seem to make it very far in the setup before receiving an error like this upon starting the server:
var parsed_url = url.parse(request.url);
TypeError: Cannot read property 'url' of undefined
This is being thrown from within the middleware.js file of the asset_builder module itself. I'm guessing this is due to the fact that Express apps no longer extend server, and so the setup in the base (app.js) file is a bit different than it once was.
Ideally I'm just trying to use asset_builder to grab all my javascript and serve it as a single file rather than the many I currently have (Ember app).
The base project I'm working off of uses ejs as the express view engine (instead of the default Jade).

Categories