dingalapadum
dingalapadum

Reputation: 2177

Node-App-Architecture: where does express come into node.js

I'm trying to teach myself how to use nodejs and building a webApp with it. In most examples I come around expressJs is used as webFrameWork. I tried to read about it, but I mostly find examples. I want to understand where exactly expressJs comes in when using nodejs.

I see how to create a helloworld-web-server with 'pure' node-js (http://howtonode.org/hello-node).

I also see how to make this with expressJs: http://expressjs.com/starter/hello-world.html

Is express simply kind of a wrapper to be able to avoid that http-boilerplate code? If so, to what extent should I use express and to which should I be using nodes-API? Is it simply used to do the routing? Or what else is there?

In the Express-FAQs I saw it doesn't help to take care of the database and that no models are created using expressJs. So, when building an App with node and express how should I handle the data (users, posts, whatever,...)?

I tried to do a whole lot of reading, but I'm not having a clear big-picture at which point one layer of abstraction ends and where the next exactly starts. I mean I understand that expressJs is a webFrameWork that sits on the node webserver running on a V8-engine, however I am a bit confused of what the final app code should be architectured: do I kind of have intermixed nodeJs code with expressJs code in a single app.js? Or should I have specific files which require express and another one for the database which does not require it?

So now you understand what I mean with: Where does express come into node.js? What does it do for me which I don't get from 'pure' node and what should I not expect from it?

Maybe my whole confusion comes because the whole thing is javascript and you don't have like these explicit 'language-barriers' which make things a bit more obvious (I'm thinking about the LAMP-stack for instance). Or is this exactly the way I'm supposed to think about javascript in general? Like the only thing which you actually get is a javascript-interpreter and everything else is lib that we can include and and expressJs simply has nodeJs as a dependency? I'm really sorry for being so fuzzy. I'm just trying to get my mental-model straight.

Upvotes: 2

Views: 1020

Answers (5)

Sachin Sharma
Sachin Sharma

Reputation: 585

Is express simply kind of a wrapper to be able to avoid that http-boilerplate code?

Yes

If so, to what extent should I use express and to which should I be using nodes-API?

All Request and Response handling should be done via Express. Also see how Express Router simplifies things when there are a lot of urls to be served in your app

Is it simply used to do the routing? Or what else is there?

Yes that's pretty much it. But that's the point. Do one thing but do it good.

So, when building an App with node and express how should I handle the data (users, posts, whatever,...)?

Take a look at Loopback

I am a bit confused of what the final app code should be architectured

You (like myself) seem to be coming from a J2EE / Spring or Dot NET world where the various frameworks try to address the complete landscape of 2 tier or 3 tier architecture view - model - dao.

Here, the landscape is little different. The frameworks are designed on the principle that do only one thing and do it good. So the overall solution becomes much tougher to arrive at.

So now there are

  1. Multiple independent frameworks (node packages) available addressing the same problem space and
  2. All these components have to work together to achieve the final big-picture solution.

So there are no crisp boundaries of how the stitching of these packages will be done. Its fluid. And its totally dependent on your peculiar problem. So no generic solutions here and no clear boundaries of what will end where and what will begin where.

You have to decide (and that's hard work). This decision making will be a process where you evolve your architecture instead of creating it upfront in a big bang effort.

It will be met with multiple failures and frustrations in initial days/months.

And then read about Javascript Fatigue

I'm just trying to get my mental-model straight.

But you are not alone. :) Mine isn't straight either. Not many people in the industry have it straight I can assure you.

Upvotes: 3

Sentient
Sentient

Reputation: 2263

As an analogy:

express is to node as

  • asp.net is to .NET
  • j2ee is to java
  • ...

Express is just server side middleware framework implemented in javascript executed by the V8 javascript engine on the server side.

Upvotes: 0

hohoho
hohoho

Reputation: 230

Express speeds up your development effort and helps to better structure the layout of the code for you. If you are new to node.js, you can build an express server in no time with minimum effort. It is also easily extendible. Usually people use a few others popular node-modules in conjunction with express-- connect, passport, ejs, body-parser, connect-redis, mongoose etc depending on what your usage scenarios are and what integration you have with other servers.

Upvotes: 0

Clark
Clark

Reputation: 406

In essence, Node is just a JavaScript interpreter, packaged with a bunch of helper libraries and a command line interface tool. Node is packaged with a bunch of utility libraries for performing common development tasks, like interacting with a file system or sending an HTTP request. Connect is an abstraction on-top of Node, providing a middleware layer. Express is an abstraction on-top of Connect & Node, that makes it easier to build certain types of applications, primarily web applications that use http, websockets, complex routing, session and cookie management, authentication, etc.

There are some competitors at the 'same level of abstraction' as Express — one of the more popular is Hapi (very similar to express), and if you are looking for a Rails-like framework on-top of Node, then look at SailsJS, Locomotive, or Meteor.

Upvotes: 0

Austin Pocus
Austin Pocus

Reputation: 993

Node itself basically lets you write Javascript on the server, and not much else. It has some basic HTTP functions, but I wouldn't want to use those functions alone to make a web app. Express is like the Rails equivalent for the Node ecosystem (with less magic). It handles routing, cookies, and most of your other web-related stuff.

You might want to check out Connect, the de facto standard library for middleware. As for data modeling, most people in the Node world use mongodb with Mongoose. In practice, you'll end up using Node built-in functions where it makes sense, and plugging in small libraries where you need other functionality. If you have any other questions, I'm happy to help.

Upvotes: 1

Related Questions