I am currently getting my feet wet using Express. To start out, I used express-generator to scaffold a simple app.
While examining the project, I noticed that the npm start command is mapped to a binary (bin/www). Upon further inspection I noticed that this file actually contains code to be executed in Node, hence the #!/usr/bin/env node pragma. For anyone having a deeper understanding of Express/Node the answer may be obvious, but still I am wondering: Why didn't they simply use a .js file to bootstrap the framework. That file could then be run using node www.js, I imagine.
There are probably a few reasons why the script was made an executable
npm scripts can be mapped to execute local JS files in the project or executables on the system.
By mapping npm start to bin/www it is effectively the same as running ./bin/www on the command line with the important distinction that by running it via a npm start, it will also work cross platform (e.g. on systems that ignore the hashbang statement, like Windows), otherwise you would need to run it as node bin/www on those systems.
There's a binary ready to add to startup scripts.
Related
I am trying to write a Node app that, once deployed, will log to Windows Event Log.
I looked through available packages and was able to successfully use node-windows to write logs to the Windows Event Viewer when I ran the app using the command line. However, when I used pkg to turn the app into an .exe file and tried to run the .exe file, it was no longer logging to Windows Event Log.
As an example, I tried writing a basic app as follows:
const EventLogger = require('node-windows').EventLogger;
const log = new EventLogger('TestApp');
log.info('Test test test!', 1000)
If I run this app using the command line (e.g. node index.js), it logs to Windows Event Viewer. However, when I run pkg to convert the project into an executable file and try to run the executable file (both as an instance and using Windows Task Scheduler), it no longer logs out.
I have already checked to ensure that the .exe is running as administrator, so I don't think it is an issue with permissions. Anyone have any thoughts on why the .exe may not be logging out? Are there any other NPM packages/libraries that provide the ability to log to Windows Event Log?
Thank you in advance!
pkg packages js source files and other assets into an EXE, and exposes them within a node process using a virtual file system. This works fine for most things, but breaks if any of those assets need to be used outside of your process.
For most of its functionality, the node-windows module distributes a few executables of its own or uses Windows builtins. It exec()s those EXEs on your behalf.
For the helpers distributed by the module, the EXE must exist on the real filesystem in order to exec() it. It is highly likely that pkg breaks this; you'd need to distribute those EXEs and place them on disk yourself.
Event logging uses the builtin eventcreate, which should work, but maybe pkg is doing something weird with the virtual filesystem that breaks how child_process works.
At any rate, I strongly recommend against using node-windows for logging because it exec()s a new child process for every log call. This is highly inefficient on Windows: it has to spawn a shell (cmd.exe) and then run the helper (eventcreate.exe), and process creation on Windows is slow. Painfully slow.
If you start logging enough things, you'll soon bring your entire system to its knees with process bookkeeping.
Instead, use a native module that calls the ReportEvent API directly. windows-eventlog fits this bill.
You'll also want to consider pkg's notes regarding native modules.
Native addons (.node files) use is supported, but packaging .node files inside the executable is not resolved yet. You have to deploy native addons used by your project to the same directory as the executable.
I am writing a nodejs application with Angular as my front end.
For this I am having Git as my code management server.
For client, I am doing minification and it is ready for production.
But I am not sure, how to prepare server side files for production.
Do we need to just copy all the Git folders into production server?.
Let me know the best way to deploy nodejs server application.
You could use pm2 as your daemon to keep your nodejs app up all the time.
Try not to include node_modules in the repo, cause different machines have different setups/installations, you cannot tell if one package would work before you run it unless you npm install them.
If you are familiar with Docker, use it, pre-bundle all (include node_modules) files into the docker image, and you do not need pm2 here, Docker itself can restart automatically. This is the ideal approach.
It really depends on how you (or your company) want to organize the workflow and the size of the project.
Sometimes I too use a GIT repository, because then is really simple to update: just a git pull and (if server files got edits) a pm2 restart N command.
In this way, you dont have to install the whole development stack in order to compile (and minify) the bundles - I guess you work on your local machine where all the development tools are installed.
Keep in mind to use the --dev flag while installing packages that are only required in development mode, so you can keep the production server as slim as possible.
A good practice I found is to add some random tokens inside the final bundle filename (both for js and css) that get then injected inside the final html static files, to avoid the refresh the page loop.
Once you have the bundle files on your dev machine, just upload them to the server (ftp, git, rsync, sshfs mount, whatever you like) and (if server files got edits) restart/reload the node process (Im using pm2 for this, its really great). If you only edited client files, no reload is needed.
Starting from here, there a lot of ways more or less sophisticated to do the job, like git pipelines for example.. but depends on the situation.
Edit: this is a good article about task runner (gulp vs grunt vs vanilla npm), while may be a little off topic, it analyze some aspect of the common deployment process
I am very much beginner in cocos2d-js game development, I am learning it past two days. I tried to run the my project through cocos command BUT its showing cocos command is not recognized as internal or external command. I have followed this link to create and run the project http://cocos2d-x.org/docs/manual/framework/html5/v2/cocos-console/en
My project directory is D:\Mallikarjun_Disk\cocosAndroid\cocos2d-js-v3.0\cocos2d-js-v3.0\tools\cocos2d-console\bin\Projects\MySimpleGame
I moved to this directory to run my MySimpleGame in command prompt BUT its throwing error 'cocos' is not recognized as an internal or external command, operable program or batch file.
Please give suggestions what might be the problem.
Giving me a simple steps for running project would be much appreciated
You've most likely:
A. Not ran setup.py from your installation folder.
B. Already ran it, but haven't rebooted your system yet.
(Note that you need to have previously installed Python v2.x, Ant -and possibly CMake- in order for setup and all other commands to work).
If you've already done this and still get the error, make sure in your System's Environment Variables that the path to [your cocos2d-js install folder]\tools\cocos2d-console\bin has been added to your PATH.
It'd also be best if you let your project and cocos2d installation folders in two separate folders with short names and no spaces, for example:
C:\cocos2d-js
C:\cocos2d-projects\MySimpleGame
In cocos2d-x v3.2 for cpp i got same error.. it turned out
i did not run
source /Users/your_user/.profile (so that environment variables are actually updated)
this line is generated after you run setup.py
All steps:
Run setup.py
Run source /Users/your_user/.profile (so that environment variables are actually updated)
Run cocos new mygame -p com.your_company.mygame -l cpp -d /directory_to_your_game
replace cpp with js in above command
I was looking over Typescript and was a bit confused about how you could build your js files from the ts files via the command line.
It implies in the documentation that you can do it easily through nodeJS, which would be great if I wanted a dependency on nodeJS... So is there any way to compile it via the command line without having nodeJS or visual studio?
This may seem crazy to some, but I would just put a build script step to output the javascript at the end if possible then package it into my release, as I tend to do most of my javascript development with RubyMine and don't want a dependency on nodeJS or Visual Studio for my build server.
If you install the TypeScript Tools without Visual Studio installed on the machine, tsc.exe and its dependencies will still get installed.
You can also just xcopy deploy tsc.exe (I don't have a definitive list of its dependencies, but it's pretty straightforward to figure out, or just copy everything that gets installed to the SDK folder) to a build server. The only thing unexpected you would need is msvcr110.dll, which you may or may not need to copy to tsc.exe's path.
The link in Ryan's answer is now heavily outdated and if you use it will generate a TS1005 error.
Here's what you want https://www.microsoft.com/en-us/download/details.aspx?id=55258 and it is still put in the same Program files x86 / Microsoft SDKs directory, and for me at least was not added to my path.
Is it possible to compile a JS application and the NodeJS interpreter into a single executable for distribution?
you need a linux box with git and python, then ugly solution:
$ git clone git://github.com/ry/node.git
$ cd node
$ vim src/node.js # add your code to end before "process.loop();"
$ ./configure
$ make
$ sudo make install
$ node
Is it possible to compile a JS application and the NodeJS interpreter into a single executable for distribution?
This might sound obvious, but here's my take on it.
A "single executable for distribution" sounds a lot like an installer...
An installer would contain or be able to fetch online your js scripts and a compiled node.js. It would unpack everything and create a script in /etc/init.d/ to start and stop the server.
If all your clients are on the same distro (e.g. Debian), I'd just make a package for the appropriate packaging tool (e.g. apt) and let the package tool handle everything.
It the clients all have different distros, you could look into autopackage.
If your goal is to execute javascript, you might be able to create a simple C or C++ wrapper program which would spawn an interpreter and evaluate your JS. If you want a single file, the js source could be included as a string constant.
When you compiled the wrapper program, you'd want to statically link it to node and the rest of its dependency tree. Rather than depending on shared libraries on the system, static linking will copy the routines your project depends on into the compiled binary.
How you do this will depend on your environment