While learning node.js I read that
We use require directive to load http module and store returned HTTP instance into http variable as follows −
var http = require("http");
I want to know what does http module means ?
An easy way to think of modules is to think of them as libraries. They add additional functionality into your application based on which modules you decide to import and use.
The http module is most beneficial in nodejs when you need to make a request over the hyper text transfer protocol. For example, if you want to send a post request or get request to a specific url, you can't use something like ajax which only works in the frontend. You will need to use the http module in order to do something like this (or use a related module but http is one of the more common ones).
The http module also has additional functionality such as creating a server or managing sockets. I recommend looking at the api for specific details- but if you are doing anything related to sending data over http: the http module is definitely worth looking into.
The easiest way to explain will be to tell you that the http module enables you to make requests to servers. If you however will like to get a proper understanding of how the node http module works then I will refer you to the documentation here https://nodejs.org/api/http.html
The documentation might seem be a bit heavy if you do not like to read but it does go very deep which is why I recommend it.
There are several ways to accomplish the module pattern like ‘Anonymous closure’, 'Global import in JQuery Module', 'CommonJS required', AMD, UMD etc. Mostly, these module patterns mainly returns the reference to the object. So in CommonJS 'required' is for using the module where as 'export' is used for exporting the module. So when we say var http = require("http"); Here, require("http") invokes javascript module loader to check for existence of mode with name "http". if he finds it it returns the reference to that module and we store it in variable http. There is good article, Writing Modular JavaScript With AMD, CommonJS & ES Harmony which explains different module loaders in javascript and CommonJS require/export
Related
I'm working on an Electron app that heavily uses the Nodejs request module. However I need to add support for proxies. This looks to be built into Electron namely in it's net module.
Net looks similar to the request module. Is it possible to substitute it with request? i.e. To replace request completely by net?
I'm writing my first Node/React/Express/Next app.
I'm uncertain how to execute server side code when a request comes in.
When there's a request, I need to get some data off the server disk. I'm trying to do this from a javascript file in my pages directory, but the 'fs' module is not accessible from here. I guess that's because the browser must be executing the code (which is confusing for me because I thought next.js meant the server was processing the code and exporting the html back).
I've done much googling about this problem and can't seem to find an answer. Which is bizarre, because this must be one of the most basic requirements of a web app. Maybe the solution is so basic and I'm just overlooking the obvious?
Can anyone provide a brief explanation or pointers in the right direction on how to do server-side processing (eg reading a file from the servers disk) from a /pages/xxx.js file in a React/Express/Next app? Or in other words, how or where do I access the 'fs' module?
The error I get is "ModuleNotFoundError: Module not found: Error: Can't resolve 'fs' in /pages
thanks
Your next.js app is split in two pieces, the part running on the client and server (rendering the pages to HTML) and the part running only on the server (finding the pages and exposing them over HTTP).
What you want to do is possible only on the server and usually done in the realm of express, the routing framework. Next.js is designed to make it easy to create SPAs that do not need a backend but want server-side rendering.
However it does allow modifying the server-only component, if you need to. The documentation for that can be found in the "Custom Server and Routing" section of the docs.
You can then add API routes that the front-end can call to request data form the backend, e.g. by returing JSON that the app can store in the React state.
I'm learning NodeJs from: http://www.tutorialspoint.com/nodejs/
And I cant understand what is the difference between using http module (get/post methods) vs using express module (get/post methods)
It seems that express module is rapid for development.
Are there advantages to use http module compared to express module ?
Are there advantages to use express module compared to http module ?
Thanks
Express is not a "module", it's a framework: it gives you an API, submodules, and methodology and conventions for quickly and easily tying together all the components necessary to put up a modern, functional web server with all the conveniences necessary for that (static asset hosting, templating, handling CSRF, CORS, cookie parsing, POST data handling, you name it, it probably lets you use it).
The http API that's baked into Node.js, on the other hand, is just the http module: it can set up connections and send and receive data, as long the connections use the hypertext transfer protocol (with the relevant HTTP verb) and that's... well that's it. That's all it does.
They are completely different things. As many articles that you can find by searching the web for the details on both will tell you.
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/
What is the difference between Google's urlfetch over the python lib urllib2 ?
When I came upon Google's urlfetch I thought maybe there were security reasons. Perhaps Google is safer in terms of malicous urls or something?
Is there any reason why I should choose Google's urlfetch over urllib2?
Note that in GAE urllib, urllib2 and httplib are just wrappers around UrlFetch (See Fetching urls in Python).
One difference of the urlfetch module is that provides you with an interface for making Asynchronous requests.
I don't work for Google, so this is just a guess from various GAE posts I've read. App Engine instances don't face the internet directly, but are buried behind layers of Google infrastructure. When a browser makes an HTTP request, it doesn't go straight to your instance, but rather it hits a Google edge server which eventually routes the request to a GAE instance.
Likewise when making an HTTP request out, your instance doesn't just open a socket (which urllib2 will normally do), but rather it sends the HTTP request to some other Google edge server which goes makes that HTTP request. Using urllib2 on GAE will use a GAE specific version which runs on top of urlfetch.
There is no problem to use standard libraries in App Engine. Url Fetch Api is just a service to make HTTP request more "easily" than urlib2. It is more understable for a novice in Python and you can easily use a non blocking request for example.
I suggest you to read some complementary information here: https://developers.google.com/appengine/docs/python/urlfetch/overview
If google found some security problem on a Python standard libraries. I guess It will send a fix ;)
The difference is : urlfetch only has a functional interface and urllib and httplib have a OO interface. An OO interface can be very usefull. I have seen a good example in the oauth2 client lib, where the request instance is passed to the client lib to check if the token is valid and authorized.