Simplest Node.js server is just:
$ npm install http-server -g
Now you can run a server via the following commands:
$ cd MyApp
$ http-server
If you're using NPM 5.2.0 or newer, you can use http-server
without installing it with npx
. This isn't recommended for use in production but is a great way to quickly get a server running on localhost.
$ npx http-server
Or, you can try this, which opens your web browser and enables CORS requests:
$ http-server -o --cors
For more options, check out the documentation for http-server
on GitHub, or run:
$ http-server --help
Lots of other nice features and brain-dead-simple deployment to NodeJitsu.
Feature Forks
Of course, you can easily top up the features with your own fork. You might find it's already been done in one of the existing 800+ forks of this project:
Light Server: An Auto Refreshing Alternative
A nice alternative to http-server
is light-server
. It supports file watching and auto-refreshing and many other features.
$ npm install -g light-server
$ light-server
Add to your directory context menu in Windows Explorer
reg.exe add HKCR\Directory\shell\LightServer\command /ve /t REG_EXPAND_SZ /f /d "\"C:\nodejs\light-server.cmd\" \"-o\" \"-s\" \"%V\""
Simple JSON REST server
If you need to create a simple REST server for a prototype project then json-server might be what you're looking for.
Auto Refreshing Editors
Most web page editors and IDE tools now include a web server that will watch your source files and auto refresh your web page when they change.
I use Live Server with Visual Studio Code.
The open source text editor Brackets also includes a NodeJS static web server. Just open any HTML file in Brackets, press "Live Preview" and it starts a static server and opens your browser at the page. The browser will auto refresh whenever you edit and save the HTML file. This especially useful when testing adaptive web sites. Open your HTML page on multiple browsers/window sizes/devices. Save your HTML page and instantly see if your adaptive stuff is working as they all auto refresh.
Web / SPA / PWA / Mobile / Desktop / Browser Ext Web Developers
Some SPA frameworks include a built in version of the Webpack DevServer that can detect source file changes and trigger an incremental rebuild and patch (called hot reloading) of your SPA or PWA web app. Here's a few popular SPA frameworks that can do this.
VueJS Developers
For VueJS developers, a favorite is Quasar Framework that includes the Webpack DevServer out of the box with switches to support server-side rendering (SSR) and proxy rules to cure your CORS issues. It includes a large number of optimized components designed to adapt for both Mobile and Desktop. These allows you to build one app for ALL platforms (SPA, SPA+SSR, PWA, PWA+SSR, Cordova and Capacitor Mobile AppStore apps, Electron Desktop Node+VueJS apps and even Browser extensions).
Another popular one is NuxtJS that also supports static HTML/CSS code generation as well as SSR or no-SSR build modes with plugins for other UI component suites.
React Framework Developers
ReactJS developers can also setup hot reloading.
Cordova/Capacitor + Ionic Framework Developers
Iconic is a mobile only hybrid component framework that now supports VueJS, React and Angular development. A local server with auto refresh features is baked into the ionic
tool. Just run ionic serve
from your app folder. Even better ... ionic serve --lab
to view auto-refreshing side by side views of both iOS and Android.
Best Answer
The first line with
start /min cmd /k
is completely useless in my point of view. Remove it.A batch file is executed by the application defined in environment variable ComSpec. ComSpec has usually the value
C:\Windows\System32\cmd.exe
. The commandstart
is (nowadays) an internal command ofcmd.exe
to start an application as a separate process.You use
start
to start one morecmd.exe
with the option to keep the minimized window open even after all applications started by this command line process finished. So all you get with the first line is a new minimized command prompt window doing nothing than waiting for user input.cmd.exe
used to execute the commands in the batch file continues immediately parsing the second command line which begins withmongod
.mongod
is not an internal command ofcmd.exe
. Thereforemongod
is interpreted bycmd.exe
as the name of an executable file.But this file name of the executable is without file extension and without path. So
cmd.exe
must search for an executable.The environment variable PATHEXT contains a list of file extensions separated by semicolons for executables. This list is used now to find
mongod.com
ormongod.exe
ormongod.bat
ormongod.cmd
, ...So my first advice for you is: Specify
mongod
with file extension, i.e.mongod.exe
As there is no file path,
cmd.exe
searches first in current working directory formongod.com
ormongod.exe
or ... and next in all directories specified in environment variable PATH separated by semicolons.PATH contains a list of directories. But there is not only one PATH. There is system PATH and a PATH for the current user account as it can be seen in Advanced system settings in Windows Control Panel after clicking on button to open the dialog for viewing and changing the environment variables.
The PATH used by all applications is a combination of system PATH and used user account PATH.
You get the error message
as the directory containing
mongod.exe
is either listed in user PATH of your user account, or could be found in current working directory on running the batch file manually by you. Butmongod.exe
is not specified in system PATH nor PATH of the account used to run this batch file as service. And the working directory on execution of the batch file as service is also a different one, usuallyC:\Windows\System32
to get working batch files if just standard applications of Windows are specified in the batch file without file extension and file path.The solution is therefore quite simple:
Specify
mongod.exe
with full path and in double quotes if the path contains one or more spaces.If
mongod
is a console application and not a GUI application, andmongod
itself also does not start a separate process and then terminates immediately before this separate process finished, you do not need anything else. The batch file with those 2 lines is all you need.Otherwise you would perhaps need:
For help on command
start
enter in a command prompt windowhelp start
orstart /?
.As a beginner in writing batch files take a look on Microsoft article about the Windows Commands.