Created
August 12, 2014 10:39
-
-
Save Hecatoncheir/183c83b7382139135258 to your computer and use it in GitHub Desktop.
Nodejs server with npm library and Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is just AWESOME!!! | |
I just been able to make a server using nodejs and dartjs and calling npm library. I decided to share my find with you guys. | |
To be able to access npm library, you need to be able to access require. Until now, it was block, but by doing this little hack, you can call require and access npm library. The tricks is to put nodejs local variable in a global variable that you can access later. | |
#main.pre.js | |
GLOBAL.nodejs = {}; | |
GLOBAL.nodejs.__dirname = __dirname; | |
GLOBAL.nodejs.__filename = __filename; | |
GLOBAL.nodejs.exports = exports; | |
GLOBAL.nodejs.module = module; | |
GLOBAL.nodejs.require = require; | |
#main.bat | |
@echo off | |
CALL C:\Users\Administrator\AppData\Local\Google\Dart\dart-sdk\bin\dart2js.bat main.dart -o main.js | |
CALL COPY main.pre.js main.pre.js_ | |
CALL TYPE main.js >> main.pre.js_ | |
CALL DEL main.js | |
CALL REN main.pre.js_ main.js | |
CALL node main.js | |
Change the path to Dart to whatever is yours. | |
#main.dart | |
import 'dart:js' as js; | |
void main() { | |
var express = js.context['nodejs'].callMethod('require', ['express.io']); | |
var app = express.apply([]); | |
app.callMethod('listen', [3413]); | |
} | |
From there, you can add more library and do whatever you want. | |
Everytime you want to run your app, you just have to execute main.bat | |
I used express.io, but it can be anything. It can be the standard http or any other library. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment