简体   繁体   中英

Can I use the Node.js packages along with Django?

I think I haven't understood this concept right . If I am using Django as a backend to run server side code , is there a way to include the packages of Node.js also .

Isn't Node.js kind of another environment or language for server side code ?

If I can use Node packages with Django , how to go about it. What does it mean when people say "Node js is a platform and Django is a framework" ?

I would be very greatful if you include some indepth details about these two environments ( new to web development here:))

Can I use the Node.js packages along with Django?

No. Django is a Python framework and thus runs in a Python interpreter. That interpreter cannot run node.js modules because those are Javascript and rely on the node.js Javascript engine.

If you want to compare things:

node.js <==> python    (runtime language engines with built-in libraries)
express <==> django    (frameworks that run in a given runtime)

This is kind of confusing because Node.js is a server-side javascript platform

Node.js is a Javascript programming environment. It can be used to write servers, but it can also be used as a general purpose scripting environment to do anything you may want to do on your computer, such as implement various build tools.

webPack is one such build tool that is written in Javascript to be run in node.js. Its function happens to be packaging client-side Javascript files, but it could be any type of tool.

There are many tools written in node.js, particularly tools that are often used by node.js developers (since they already have that environment installed).


If you really needed to combine functionality from both node.js and django, then you would have to create two separate programs 1) a python program using django and 2) a node.js program using whatever Javascript libraries you want and then you could communicate between the two programs using whatever IPC mechanism you choose (TCP, stdio, files, etc...).

While you'd need two different environments to use nodejs as a server along with django as a server, node.js has a critical role in managing packages for client Javascript in modern web development. As an example, tools like Webpack will bundle a series of Javascript modules for a client. One of the more convenient ways to package these modules and their dependencies is using npm , the Node package manager. So, it would be entirely reasonable to use Node to bundle the Javascript for your client and even to install the modules for bundling. This is especially true if you're using a framework like Angular on the client. So, if you had an Angular application backed by a Django server, your work flow might look like the following:

  • Create a node project with your Angular App

  • use npm to install and manage its dependencies

  • use ng build --prod to call webpack to produce a bundle that could be sent to the client.

All the above would use Node.

Then:

  • Write your models and business logic for the server in Django

  • use some Django restful framework to present a REST API that your client application can call.

This is kind of confusing because Node.js is a server-side javascript platform, but it's being used to:

  • Provide packaging for client modules

  • provide server-side transformations to prepare the content that your particular site sends to the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM