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.
Related
I'm still new to the world of Node frameworks. I chose Preact because it seemed to have less dependency than React.
I think I originally misunderstood what exactly this is. I thought it works like PHP: we do tasks on the server page and the client gets the result in HTML form. I understand this is a framework that does not require a Node.js server, but when I create a project using Preact-CLI I can only run it on one Node server (at least opening the generated HTML file does not work by itself, the page does not respond when buttons are pressed, even if I change the file paths from absolute to relative).
I would like to read files in the Preact app, run MySQL queries, but these require Node modules. If const fs = require('fs'); is included in the Preact component, npm start will drop this error:
Module not found: Error: Can't resolve 'fs' in '...'
How do I get Node modules to work, read files, etc.? Should I use AJAX with a separate server (with completely separate code) and communicate with Preact?
Node frameworks => You mean JS Frameworks :)
Preact is a library for the browser, not server, and as such does not have access to the filesystem and other stuff.
To read e.g. from a SQL database, you have to make a call to an API that queries the database and sends the result back to the client (browser).
Preact is a clientside framework like React, it's not a server side rendered framework.
You'll need to make api calls to an external server from your Preact app.
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.
This query is more related to nodejs(compared to particular os) thats why posting it here.
I have searched for similar problem, but I did not get any satisfactory results.
I want to know that Is there any way that we can figure out the JS Script(name, location) node is running currently?
e.g.
If I am running a nodejs webserver on port 3000, and if I forget the original script location, or somebody else start using my computer.
So, is it possible using nodejs to get the location of this script.
Try the global variable __filename, as detailed here.
I'm trying to figure out how best to architect my app. But for starters trying to understand typical practices with respect to where to put things, and how the app should wire up to things like server.js, how server.js should work, and how you keep a persistent connection open for the website, and any backend services or modules.
This is a general question but let me try to be more specific, as specific as I can since I am new to Node.. and for a basis to start on with this question.
Lets say I plan on designing a simple Express App.
I've got this kind of structure for example so far:
Right now in server.js, I am just playing around with trying to connect to a mySQL database. So I've got a connection pool I'm creating, one call to the store to retrieve data, requires at the time for the node-mysql middleware I'm using, etc.
app.js just has very simple code, it's not modular yet, or even production ready but that's just me playing with the code, spiking things out. So in it I have you're typical stuff like setting the view, var app = express();, importing an express route definition from another module in my routes\index.js, stuff like that.
if I'm going to keep a database connection open, or other things open, how is that best organized/done by convention?
If you look at this example code, he's moving the var app = express() definition into service.js: https://github.com/madhums/node-express-mongoose-demo/blob/master/server.js. He keeps an open connection running and started from there, which makes sense, hence "server".
so should app.js do much? what is best practice or scope of what this should be doing. Once I start modularizing things out into their own .js files and node modules, how does the app.js morph through all those refactorings, meaning in the end what's its role and is it very thin in the end where it's just used to wire stuff up?
Then what should www.js which is now required by express 4 have and it's role?
It's kinda hard for me to start with just one aspect so I'm kinda going all over the place here in the above. I just want to know common conventions for putting stuff in app.js vs. server.js and then best way to keep and managed open connections to things...both in the backend and front-end such as http requests coming in, what should be the central point? routes of course but then so is app.js responsible for referencing routes?
I have found a few resources such as this but looking for more so if you know any or have any input, please reply. I'm more interested in the talk around app.js, server.js, connections, www.js, and where things should wire up to each other with these particular specific parts. I realize the rest is up to you on how you wanna name folders, etc.
There is no right way and (arguably) no wrong way. There are which are better than others, but then someone might say that they don't like this way and you should do it the other way and so on, until your project is over the deadline.
I often refer to this blog post about best practices when developing an express app.
You could also try one of yeoman generators. Choose one that suits most/all of your needs.
Bottom line, there is sadly still no answer to best structure of an app, I would recommend you to pick something that works best for you (and your team) and stick with it. Consistency is the most important thing to keep in mind while developing and JavaScript community it clearly lacking it.
I'm creating a module that exports a method that can may be called several times by any code in node.js using it. The method will be called usually from views and it will output some html/css/js. Some of this html/css/js however only needs to be output once per page so I'd like to output it only the first time the module is called per request. I can accomplish doing it the first time the module is called ever but again the method of my module can be called several times across several requests for the time the server is up so I specifically want to run some specific code only once per page.
Furthermore, I want to do this while requiring the user to pass as little to my method as possible. If they pass the request object when creating the server I figure I can put a variable in there that will tell me if my method was already called or not. Ideally though I'd like to avoid even that. I'm thinking something like the following from within my module:
var http = require('http');
http.Server.on('request', function(request, response){
console.log('REQUEST EVENT FIRED!');
// output one-time css
});
However this doesn't work, I assume it's because I'm not actually pointing to the Server emitter that was/may have been created in the script that was originally called. I'm new to node.js so any ideas, clues or help is greatly appreciated. Thanks!
Setting a variable on the request is an accepted pattern. Or on the response, if you don't even want to pass the request to your function.
One more thing you can do is indeed, like you write, have the app add a middleware and have that middleware either output that thing.
I'm not sure if I completely understand your "problem" but what you are trying to achieve seems to me like building a web application using Node.js. I think you should use one of the web frameworks that are available for Node so you can avoid reinventing the wheel (writing routing, static files serving etc. yourself).
Express framework is a nice place to start. You can find tons of tutorials around the internet and it has strong community: http://expressjs.com/