node.js module or require.js - node.js

i wonna programming web app and using express.js as webframework. As testing framework i will use mocha and chai.
Node.js support modular programming, i can load module with require like require("mocha"). On frontend i use require.js for organize my codes in module, but this can be use in node.js to. It it better to use require.js in node.js or better to use node.js standard module.
I know that require.js support AMD and testing codes with mocha it can comlicated, because the codes will be load asynchronously.
Node.js require or require.js for module loading?

node.js supports CommonJS module format.
If you have modules that you want to use both in browser and node.js, you can use require.js in node.

Related

difference between node.js and express

I'm new to the front end development and modern technologies MEAN/MERN stack, I am confuse about difference between node.js and express can someone outline differences or advantages
and one can learn express.js directly or it's a pre requisite to learn node.js first?
Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It is used to run JavaScript in ways similar to how the browser runs JavaScript but on servers. Express is a library (called a package in the Node.js ecosystem) that makes it really easy to build APIs or serve files.
Node.js is a platform for creating server-side event-driven I/o application using javascript.
Express.js is a framework based on node.js for developing web-application using principles and methods of node.js.
In simpler terms, express.js makes handling API requests and server management easier than using only node.js
Express js is built on top of the Node.js framework.
Express js uses node.js itself and provides more features to build applications on top Node.js
Node.js: Node.js is a Javascript runtime environment for executing Javascript code outside of a browser.
Express.js: Express.js is fast and light-weigh framework for building web applications.
You should first learn Node.js and it's core modules like path, fs, os, events and others.
After that you should learn asynchronous Node.js like callback, Promises and async/await.
Then you should learn express framework.

How to use a Nodejs module with AMD require

I use monaco-editor in an electron app,and I want to add my own grammar support;
I tried to build a plugin like monaco-editor's grammar support library such as monaco-html or monaco-css, Monaco-editor use WebWorker to load those grammar lib,and here's the problem:
in the WebWorker,I can only access a build-in AMD require, I can't get any Node.js module with AMD require,any way I can get the nodeRequire function or require a Nodejs Module with AMD require?
any advise can be helpful,Thanks a lot.

Nodejs and Expressjs difference

What is the difference between nodejs and expressjs and
Is it possible to create webpage only using nodejs?
Node.js is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code server-side. Node allows you to write and run Javascript code server-side.
Express.js is a framework for Node.js. Basically instead of writing 20 lines of code for a simple functionality with Node.js, you can write it in 2 lines using Express.js.
You can write the webapp using Node.js, but I don`t recommend it. Express is a minimalistic framework and it will make you life easier.
Node.js is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code server-side. Node allows you to write and run Javascript code server-side.
Express.js is a framework for Node.js
Express is built on top of Node, so yes Express adds more features. Node is just a Javascript environment with libraries to make it easy to write software, where Express extends Node specifically to make webservers easy to write.
Express/Connect adds the concept of middleware, a simplified way of managing different routes, automated integration with several templating engines and a bunch more.
All nodejs framework in here
Help it help you.
Thanks.

How do you know what Node.js code will run on the browser?

I'm learning how to use Flux and I encountered the following line in the documentation: "We can use Node's EventEmitter to get started with a store."
I understand that you can use things like Browserify to do the bundling and minifying, grabbing all the dependencies that Node code has to make the bundled browser-compatible JS file. But what's bugging me right now is how you know what you can do this with. How do we know what Node code we're allowed to use in the browser?
So, first of all let's consider that when in node you have
JavaScript modules that are the 3rdParty modules written in JavaScript (ECMA5, ECMA6 and even TypeScript or CoffeScript) etc;
Node built-in module. Those are node Core modules like fs, path, util, etc.
native compiled module called Addons that are are
dynamically-linked shared objects, written in C or C++;
Then you have the packager / module bundlers
Browserify
Webpack
the transpilers i.e. source to source compilers that typicall will handle syntax tranforms like
Babel.js that shims modern JavaScript to legacy engines
and the techniques
ECMA5 Shim to support legacy JavaScript engines
HTML5 Cross-Browser Polyfills
Because you need to do polyfills if you want to transform not only syntax but even globals (like the Promise), so you combine transpiler to polyfill having like babel-polyfill
Finally we have different kind of modules design patterns (modules format) to be handled for the bundling process:
AMD modules format
CommonJS modules format
and formats that are not in those ones that must be bundled/shimmed - where possible - through custom loaders.
That said, native modules will not run in the browser: you cannot bundle a native module through Webpack. Ordinary modules will, but not all. This is due to several reasons. There are some specific methods that cannot be "browserified" or "webpacked". Let's take as example fs. Can you put this built-in module in the browser? There are some abstraction for that called brfs, that are transforms for built-in node apis fs.readFileSync() and fs.readFile(), so you will do
$ browserify -t brfs example/main.js > bundle.js
to get
var fs = require('fs');
var html = fs.readFileSync(__dirname + '/robot.html', 'utf8');
console.log(html);
This will not work for every non built-in modules, in the npm modules jungle, so WebPack has a module.noParse option to exclude Addons modules, non supported modules, etc. - see here about that.
So you have to look at the list of the transforms that means you can apply this transform to browserify to obtain like the fs transform mentioned above.
So that said, how do you know that a certain module will run in the browser? When you design your web application and Node backend you have to do opportunistic design choises to design shared modules/library that will run in both the environment, so being shimmed/packed at some point, like object models, application logic, etc., other modules that will deal with the File System I/O or will use native addons, so that will work in the server only, packing through wrappers it's possibile, but the behavior will look differently, as we have seen in the fs example above, and web specific modules, so it's a design matter.
A note can be added about networking modules i.e. node http, https that thanks to library abstractions like node request will run everywhere or using specific transforms like http-browserify.

How to share code between node.js server & browser using Typescript

I am planning to do a little multiplayer javascript game with a node.js server (using socket.io) and would like to share some code (mostly classes) between the webclient and the server. So I found this:
How can I share code between Node.js and the browser?
However I would also like to use Typescript and was wondering how to set it up? What tsconfig.json settings are best suited for this? Do I have to use something like browserify or webpack?
Do I have to use something like browserify or webpack?
Yes. I use webpack.
What tsconfig.json settings are best suited for this
just use commonjs everywhere.
More
here is a project in typescript with both backend / frontend .... uses webpack ... and has a single tsconfig.json : https://github.com/alm-tools/alm

Resources