What is the difference between http module and express module? - node.js

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.

Related

Do I need Express to create web services using Nextjs?

I am trying to create a general web service using Nextjs.
In my research, I often see cases where Express is used as the backend for Nextjs.
However, Nextjs has an api function.
In what cases do we need to use Express for the backend?
Well, you don't really need Express as the backend server. You should be able to use any framework from any programming language. I guess the reason that you often see it used is because it is the best documented.
Regarding api routes, you will always have to use some kind of backend server as it does not work with next export (aka creating a static folder containing html, css and JavaScript).

What is a require("http") in node.js

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

Bigger projects Node.js and RESTful API

I'm looking into node.js which really seem like a pretty nice environment. I've worked with a lot of different Technologies and for server, mainly php and Java (jsp), but dabbled in som RoR and Python.
I find node.js really easy to get up and running and it feels quite natural to work with, and I found some good entry level tutorials.
I just am missing some more intermediate resources. For example when creating bigger frameworks or api's how would you structure or architect it. I set up some smaller api's to try it out where it would go something like this:
I've made use of the Express framework to create a http server, listen to a port, set up an express object and bound some requests.
However these have been quite small, and the purpose has been learning, if I think about scaling up the size of the API for production, perhaps wanting to do other stuff like serve web-pages as well. I find it hard to see how the architecture would look.
It's vague as I am still new to node.js but I'm mainly thinking about things like if you typically keep all api in one file or if there are good ways to split it up into modules? And if anyone know any resource talking a bit more about how to design the architecture when working in node.js
Sorry for the vague question and thanks for reading.
In my opinion, Express is the good way to go if you want to build complex or big APIs.
It is among others easily testable (for instance with Mocha or Jasmine) and customizable, especially thanks to its middlewares.
For the directory structure, what I usually use is (at least) the following:
app.js : the main entrypoint. Will create the express application, indicate which controller to use for every route prefix, and assign the middlewares. Example from a previous project
controllers : will contain the controllers, the functions which will handle the requests, in the same style as in standard MVC frameworks (e.g. UserController, ...). Each controller would create an express Router object and export it. Inside the controllers, individual handlers are in charge of individual API requests, such as /api/users/list. It would use some library to access your data (e.g. Mongoose for MongoDB), and would then send the response to the client. Example (UserController.js)
models : will contain the models with all their attributes and methods. In my case, it would be the Mongoose models. Example (Song.js)
middlewares : will contain the various middlewares of the project. A practical example would be a middleware checking for an access token in the incoming request, and returning a 403 HTTP error if not. Example (AuthMiddleware.js)
helpers : various helpers
tests : unit tests of you API
This could be the minimal directory organization. On top of that, you may want to use a templating engine such as EJS to serve webpage. Take a look at « Use EJS to template your node application ».
This is only to give you an overview of what an express directory structure could look like, but there are of course plenty (better?) other possibilities. Hope that gives you a quick and useful insight :)

Using Google AppEngine Urlfetch instead of urllib2

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.

What is the difference between Node.js and adding a framework like Express.js?

I come from a networking background and I'm fairly new to web development but I want to experiment with Node.js and other modern web technologies. I would like to know what the difference is between using "stock" Node.js and adding a framework like Express.js.
node has a low level HTTP API,
Express offers useful things like routing, view engines and http handler flow control.
Basically express is a nice abstraction it also offers a set of common http handlers like routing static content or handling errors or parsing the body of a HTTP post.
It's basically a comparison of a library and a framework.

Resources