How to create an express api executable - javascript

I have created a fully functioning fairly basic javaScript Node.js Express API application that I want to run as an executable in a windows environment. I am wanting to do this so I can give clients the ability to run my API on premise without exposing my source code to them.
Currently I have been using the pkg npm package which allows me to package my node.js application into an executable that will contain everything needed to run the app including node and my bundled source code.
My executable runs but my POST route is breaking with the following error:
"name": "RequestError",
"message": "Error: form-data: File or directory 'C:\\**\\myapp-api\\uploads\\1553103249524_test.wav' was not included into executable at compilation stage. Please recompile adding it as asset or script.",
"cause": {
"errno": -4058,
"code": "ENOENT",
"path": "C:\\snapshot\\myapp-api\\uploads\\test.wav",
"pkg": true
},
"error": {
"errno": -4058,
"code": "ENOENT",
"path": "C:\\snapshot\\myapp-api\\uploads\\1553103249524_test.wav",
"pkg": true
},
My POST allows clients to upload a file in a multipart form using multer.js to another external API that will return some metadata. pkg.js doesn't appear to have the means to discover files that are included after the bundling of the executable.
Is there anything I can do in my configuration for my uploaded files to be included? Is there some other utility or process that others use for creating an executable of their node.js express APIs that would better handle the issue I am having?
Any guidance would really be great.

Try adding your files under "assets" in the package.json file.
The config paragraph on the pkg website https://www.npmjs.com/package/pkg#config states:
So you must specify the files - scripts and assets - manually in pkg property of your package.json file.
"pkg": {
"scripts": "build/**/*.js",
"assets": "views/**/*"
}
You may also specify arrays of globs:
"assets": [ "assets/**/*", "images/**/*" ]
Just be sure to call pkg package.json or pkg . to make use of scripts and assets entries.

You are probably using something like this in your script (I am guessing since you didnt provide this part of the code)
app.use(express.static(__dirname+'/uploads'));
res.sendFile(path.join(__dirname+'/uploads'));
__dirname will be wrong when you pack your .exe get rid of it everywhere and replace it with ./
app.use(express.static('./uploads'));
res.sendFile(path.join('./uploads'));
Something like that. It worked for me.
Good luck!

Related

How to include two C++ libraries - Boost and Quantlib - in node gyp?

I am new to working with node-gyp in making C++ Addons for JavaScript on Windows and have been struggling for a while with including Quantlib in my binding.gyp file. I was able to generate the .lib file from Quantlib's official site (https://www.quantlib.org/install/vc10.shtml); however, the guide did not mention anything about generating a .dll file, and I am not sure if a .dll file is needed to include Quantlib. Although I cannot include the Quantlib library, I have been successful in linking another C++ library in my binding.gyp file. Any help would be much appreciated!
My binding.gyp file:
{
"targets": [
{
"target_name": "hello",
"sources": [ "src/hello.cc", "src/test.cpp", "src/test.h"],
"include_dirs": ["<!(node -e \"require('nan')\")", "eigen-3.3.7/eigen-3.3.7", "ql/math", "ql", "Quantlib"],
"libraries": ["<(module_root_dir)/Quantlib/QuantLib-mt.lib"]
}
]
}
NOTE: eigen-3.3.7 is the C++ library I was able to include in my binding.gyp file. I created a directory in my root folder for quantlib's .lib file called "Quantlib".

Nuxt js restart server on api folder changes

In my nuxt js application, I am trying to restart the server files which is in the api folder when a file changes.
In order to that, I've added the following to nuxt.config.js
build: {
watch: ["~/api/index.js"]
}
When I make the changes on the files in API folder server doesn't restarted automatically
I've tried this thread Watch and reload api folder in Vue Nuxt looks similar to my problem but it doesn't worked for me
As suggested here
Watch and reload api folder in Vue Nuxt
just adding
watch: ['api'],
to nuxt.config.js, at the root level, worked for me. Not under build, as you had it.
EDIT
Bizarrely, although I now see the server (and client, for some reason) being started in the console... the new code is not taken! So, that's fairly useless.
The only way to get the code changes is still to ctrl-C and restart from zero.
In my application, I use nodemon.json config
{
"verbose": true,
"ignore": ["node_modules", "dist"],
"watch": [
"app.js"
],
"ext": "js json"
}

VScode sourceMaps detected but not used

Hi everyone !
I'm using VSCode to work on an OpenSource project in monorepo with lerna, and I want to provide a package which contains the tools needed to contribute easily.
In order to do this in a way that seems pretty clean to me, I added my monorepo as a submodule in my workbench package.
Here is a link to my current setup:
https://github.com/Aetherall/accounts-workbench
The main goal of this package is to give to the developper a working debugger configuration, which allows breakpoints and follows the Error stack within the sources of the monorepo packages.
I succeded to make VSCode read my sourcemaps.
Indeed, I switched on the trace options, and I saw in the logs that the .map files were resolved.
But, - and there is my issue - when I trigger an error, instead of leading me to the sources, the debugger just show me the transpiled files...
Here is a sample of my log for one file ( I can provide my whole log if needed to help me )
SourceMaps: sourcemap url parsed from end of generated content:
AuthenticationServicePassword.js.map
SourceMaps.getMapForGeneratedPath: Finding SourceMap for
/home/aetherall/Workspace/github/accounts/accounts-workbench/accounts/packages/Server/Authentication/Password/PasswordService/lib/AuthenticationServicePassword.js by URI:
AuthenticationServicePassword.js.map
SourceMaps.loadSourceMapContents: Reading local sourcemap file from
/home/aetherall/Workspace/github/accounts/accounts-workbench/accounts/packages/Server/Authentication/Password/PasswordService/lib/AuthenticationServicePassword.js.map
here is my debugger config:
{
"type": "node",
"request": "launch",
"name": "Start dev server",
"program": "${workspaceRoot}/config/start.js",
"protocol": "inspector",
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"outFiles": [
"${workspaceRoot}/dist/**/*.js",
"${workspaceRoot}/**/lib/**/*.js",
"!**/node_modules/**/*",
],
"skipFiles": ["${workspaceRoot}/node_modules/**/*", "<node_internals>/**/*.js"],
"smartStep": true,
"trace": "sm"
},
I am using webpack to bundle the workbench package (not the monorepo) and tsc to transpile the typescript packages in javascript with sourceMaps in my monorepo submodule
I can of course add more informations if needed
Thanks for helping me on this one !
I really can't get why sourceMaps arn't used by the debugger ...
If you do have a solution, please give me some explanations on the problem
I found a solution to my issue :
Source Map loader for webpack
this way, the sourcemaps of the files imported and transpiled will be resolved within webpack.
https://github.com/webpack-contrib/source-map-loader

How to deploy client side dependencies on webserver?

I have done some web developing using Python and Django. I use virtualenv to make a bootstrap script that can install all my Python dependencies on a server. I have a repository for the code I have written myself, and in that repository are two file (beside my code): requirements.txt and bootstrap.py. Using the bootstrap script, it installs all the dependencies on the server.
Now I would like something similar for the client side dependencies. E.g. jQuery, jQuery-ui and bootstrap. Currently I manually download the files and put them in the static folder on the server.
I have run into Bower, and I understand that it can indeed download various js-libraries. But I don't see how to use it in an elegant way. E.g. for jQuery it downloads the entire jQuery repository, which means both a dist folder and a src folder, containing tons of files. All I need is the jQuery.min.js.
Well, wide question, what is the neat way of automating the deployment of client side dependencies?
you can use composer
{
"require": {
"jquery/jquery": "*"
},
"repositories": [
{
"type": "package",
"package": {
"name": "jquery/jquery",
"version": "1.8.2",
"dist": {
"url": "http://code.jquery.com/jquery-1.8.2.min.js",
"type": "file"
}
}
}
]
}

Instruct Sencha SDK tools to bundle other js files specified in app.json

My app.json file of a Sencha touch 2 application contain.
"js": [
{
"path": "sdk/sencha-touch.js"
},
{"path": "js/mootools-1.2.5-core.js"}, // I want these files to be bundled too
{"path": "js/mootools-1.2.5.1-more.js"}, // <----------+
{"path": "js/soundmanager2-nodebug-jsmin.js"}, // <----+
... // <----+ and there are more.
...
{
"path": "app.js",
"bundle": true, /* Indicates that all class dependencies are concatenated into this file when build */
"update": "delta"
},
Now I see when I invoke sencha app build production It compiles all the sencha classes into a giant app.js file. But all my other classes are just compressed to build directory. They are not concatenated. how can I include them in app.js?
F.A.Q.
Your json file is properly written, right?
A. Yes, app.json is written without any syntax error. The project builds successfully on invoking sencha app build production
After looking at the source code and talking with the devs behind Cmd, it appears that it is currently not possible.
However, because the build file is written in JavaScript, in theory, it wouldn't take much to modify it and add this functionality into Cmd.
You can find the Sencha Touch build file in:
CMD-ROOT/plugins/touch/current/app-build.js
Where CMD-ROOT is the location of the sencha command - which you can find out by using which sencha.
On my system (OSX), the path is:
/Users/Robert/bin/Sencha/Cmd/3.0.0.250/plugins/touch/current/app-build.js
Hopefully this is of some help to you.
Update
It appears that, after talking to another Cmd developer, this actually is possible. There are 2 steps you need to take to make it happen:
1) Add the skipFrameworkFile property into each JS resource you want to bundle. This tells the compiler to not copy the resource when your build your app.
{
"path": "resources/js/jquery.js",
"skipFrameworkFile": true
},
"path": "resources/js/jquery2.js",
"skipFrameworkFile": true
}
2) Require each of the files in your app.js file using the #require tag. This tells the compiler to include each of your files into your app.js file.
//#require resources/js/jquery.js
//#require resources/js/jquery2.js
For SenchaCmd 3.2, rdougan's solution didn't work for me. However, instead of using:
'skipFrameworkFile: true
I used
'x-bootstrap': true
(by looking at SenchaCmd source code) and it worked!
The other steps are the same as rdougan's
Hope this helps. Cheers

Categories

Resources