I'm getting a 'fs.readFileSync is not a function' in Chrome debugger after trying to call readFileSync();
I call it...
const fs = require('fs');
call the function...
let content = fs.readFileSync('/path/to/my/file.stuff');
And attempt to display content..
console.log(content);
I get nothing. When I do...
console.log(fs);
I appear to get a generic javascript object...
I'm completely stuck.
Meteor version: 1.5.1
npm version: 3.10.10
node version: v6.10.1
Thanks for all the answers!
I have confirmed that you cannot use fs on the client side.
Instead, I made another local simple express node api and the react web app just makes a request back to the node api to get that data.
Also, you have to do this...
https://enable-cors.org/server_expressjs.html
EDIT:
Wrote this a long time ago. 3 years back when I was just starting my web development learning. Just want to update and say that there is a serious fundamental difference between what the user sees and what the server sees. Allowing the front-end (Meteor, React, Angular, etc.) to read files would be a super serious security issue. Anyone could make a website that when a user goes to it, it would just read your local computers files. Not good...
While this is super obvious to me now, it wasn't obvious 3 years ago. So for all you newbies out there, it's okay :) No question is a dumb question.
I'm getting a 'fs.readFileSync is not a function' in Chrome debugger after trying to call readFileSync();
fs will not work in the browser. This is by design as to protect your filesystem from potential security threats.
Using low level Node packages in a browser environment
If you need access to this in a browser environment, consider making use of Electron which allows you to make use of OS level NodeJS packages in a running instance of Chromium.
fs cannot be used on the client, due to browsers restricting some javascript code.
If your code is being run on both the server and client, you can use:
if (Meteor.isClient) return;
to avoid the error. Otherwise, there should be another way to do what you're trying to accomplish, such as importing required JSON.
Related
I would like to start with a disclaimer that I have no previous coding experience at all, and I have just learned HTML, CSS, JS, node.js, phaser.io, understanding all the jargon such as API, asynchronize, I/O driven, cookies & session, etc.. all that in the past 1.5 months on the web by myself, so if my question doesn't make sense please let me know.
I would ultimately like to make a cross-platform online multiplayer "webgame" (No realtime interaction between clients), I know it is ambitious but I don't mind take one step at a time, finishing the game in the period of 2-3 years on my spare time as a hobbyist, so let's move on to the actual question.
After doing all my research I decided to use phaser as client framework, PhoneGap to compile for crossplatform, and node.js + passport + mongodb for server, user authentication and storage. Since there are no realtime element between clients, and there are more tutorial on using express along with the stack of backend packages mentioned above. I am more keen on using http protocol with express then using websocket or socket.io.
Now I have created a simple login screen with input field using phaser-input plugin, and a button to send the data out. I am surprised when I cannot find any Phaser API on http request.
Q:Are there any Phaser API to do a http POST or GET request? if not is it possible to implement jQuery Ajax (does phaser include the jQuery library already or do I need to include the script as well?)? And if I should use socket.io anyway?
I am working on a phonegap project that use html as login screen too.
First. Yes and no, Phaser itself is just a framework. Phaser 's not a library. But if you want to make http request, i suggest create a Network.js that abstract all of your network call on different platform .I think you can use:
IOS: use Cordova http request.
Android: use Ajax is fine. Yes and it is possible to use jquery. Cordova, Phongap, IONic is nothing more than a web view. So you can use whatever js library you like.
Web: use Ajax too.
I'm having trouble adding the line require('fs') anywhere in ambari-web. I've tried adding it in ambari-web/app/app.js, ambari-web/app/controllers/wizard/step3_controller.js, and other places. Every time, I end up with
Uncaught Error: Cannot find module "fs" from "app"
or something similar in Chrome's console log. What is going wrong, what misunderstandings do I have, and how can I add the fs module to this ember application so that I can use it in a controller? I've tried running following all of the build instructions again after adding the module too -- without any luck.
Edit: is this a bad question? I'm getting downvoted, so let me know how to improve it.
fs is a Node.js module that is not available in browsers. There are various ways to emulate it in browsers, depending on what you're trying to do, but most likely to accomplish what you're attempting you'd need a separate Node.js program running that the web app makes requests to in order to trigger the file system operations you want to do.
I've been using Yeoman 0.9 up until yesterday when I decided to use the beta 1.0 release on OS X. I use Yeoman to develop an angular app.
With the Yeoman 0.9-->1.0 migration of my app done and working, I'm now keen to extend the dev server fired up by yeoman grunt server to allow me to make cross-domain calls to an API developed by another team, hosted on another server, where they've already allowed cross-domain calls. Up to now we've been using a fake http backend courtesy of angular.js.
--An aside--
If anyone's reading this looking for a quick solution, we got cross-domain calls working by passing the --disable-web-security option to Chrome from the command line using this approach on OS X with Chrome, but it turns off security for all of Chrome (thumbs down) and we can't get Chrome to fire up as a new instance anyway.
--End aside--
Through some digging I found this Stack Overflow post (via the answer to this post) giving me a pretty good idea on what I need to do to get grunt server allowing cross-domain calls.
It essentially involves adding a middleware component to connect to allow alter the headers so that "Access-Control-Allow-Origin" is set to "*" or whatever one likes.
Knowing nothing about Node.js, I can see the change alluded to in the post needs to be made somewhere in the many files created by Yeoman, but where? I've done some raw string searches for "app.configure" and "connect.listen" within the "node_modules" directory set up by Yeoman but I come up with a number of hits, most of which are from examples bundled with the modules and it isn't clear to me which I should edit. In case it helps, here's a snapshot of the directory structure for my angular app:
If anyone can give me some pointers on where these changes may be made I'd really appreciate it!
Solved sans messing around with node using the approach in this github pull request: http://github.com/angular/angular.js/pull/1454
You can use Apache proxy and connect your REST server with gruntjs(angular.js).
Apache would do this:
proxy / -> gruntjs
proxy /service -> REST server
you would use your application hitting Apache and angular.js application would think that is talking with itself so no cross domain problem.
Here is a great tutorial on how to set this up: http://alfrescoblog.com/2014/06/14/angular-js-activiti-webapp-with-activiti-rest/
I am trying to allow users to upload images to a site I have built with Meteor, which requires a server-side route I can POST data to. Is there a way to set up a route server side such that I can call Meteor code from it (for example, a route that I can call this.userId or Meteor.userId() from)?
Server side routing is on the roadmap, but not yet available the way you need it. (At that link, Server-side rendering is set for version 1.0).
In the interim, you can do some server side rendering with Tom Coleman's excellent meteor-router mrt package. It's unclear to me how much of this will make it into Meteor core.
More likely, however, for the file upload problem, this issue describes the problem, and it looks like people have had luck implementing imgur's xhr api or perhaps even better is this smart idea.
Hope this helps.
I'm trying to get my node.js server to server up audio files for the HTML5 audio tag. Currently I'm referencing audio files via an express staticProvider, but this will allow the sound to play once and then never again.
From what I've been able to gather in order for the sound to work properly with seeking or looping I need to provide the "Content-Range" header when my audio file is requested. I highly doubt that the express staticProvider is doing this for me, so it would seem that I need to serve the file up using custom code. Unfortunately I'm fairly new to node.js and things like serving files are still a little beyond me. Can anyone offer some advice on how I can provide the appropriate metadata for my audio files?
[EDIT] (Removed old server code since it won't do anyone any good!)
Sorry, I don't typically ask questions this broad, but I'm really at a loss as to where to start with this one. Any suggestions?
[SOLUTION]
So it appears that the solution is just to use updated software. I was trying express rather than the built-in connect HTTP middleware because I thought connect wasn't doing static file serving right. Turns out, I was just looking at the documentation for a newer version of connect while the version that comes with node is a bit older. Once I updated my connect library (I just used npm to install the latest, for those that are curious), the following worked beautifully:
var connect = require('connect');
var server = connect.createServer(
// If your server errors on this line, saying it doesn't know what
// "static" is you need to get the latest connect!
connect.static(__dirname + '/public')
);
server.listen(PORT);
The static serving logic is done by the underlying connect server (using its static middleware). There is code for 'Content-Range' headers (see here, about line 148), but these headers are only set when the underlying client (in your case, the browser's HTML5 implementation) sends the correct request headers.
Maybe this post from google groups can help you out. The topic is slightly different (video streaming), but i think the core of the problem is about the same.