Skip to content

Bundling static folder into a single JavaScript Node app bundle (example)

Notifications You must be signed in to change notification settings

bahmutov/browserify-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

browserify-server

Bundling static folder into a single JavaScript Node app bundle (example)

Install dependencies first npm install

Run in "normal" code npm start - then request public/index.html using http localhost:3000

$ http localhost:3000
HTTP/1.1 200 OK
<!DOCTYPE html>
<html>
<head>
  <title>example</title>
</head>
<body>
<h2>index page</h2>
</body>
</html>

Now bundle all code and public folder into a single JavaScript file

npm run bundle
> node build.js
{ 'public/index.html': '<!DOCTYPE html>\n<html>\n<head>\n ... </html>\n' }

The bundle dist/bundle.js contains all server code and public/index.html contents.

$ grep DOCTYPE dist/bundle.js
"public/index.html": "<!DOCTYPE html>\n...</html>\n"

The command to run the bundle changes the working folder to dist, making sure we do not accidentally serve ./public/index.html. We can even remove public folder now

mv public tmp
npm run run
running in /git/browserify-server/dist
listening on port 3000

Making the request from another terminal still works.

How

Includes the HTML contents in the bundle and during execution mocks the file system for serve-static middleware using mock-fs module.

See build.js and start.js files that wrap around index.js.

Small print

Author: Gleb Bahmutov © 2016

License: MIT - do anything with the code, but don't blame me if it does not work.

Spread the word: tweet, star on github, etc.

Support: if you find any problems with this module, email / tweet / open issue on Github

About

Bundling static folder into a single JavaScript Node app bundle (example)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published