Express gzip static content - javascript

Express and connect appeared to have removed their gzip functions because they were too inefficient. Are there any reliable solutions to gzip with express-js currently?

Express 3.0 now has compress() support:
var app = express();
// gzip
app.use(express.compress());
// static
app.use("/public", express.static(__dirname + '/public'));
// listen
app.listen(80);
EDIT
for Express 4.0, compress become the separate middleware. So you have to install and import to use it:
var compress = require('compression');
app.use(compress());

Connect 2.0 has added support for compress() middleware based on the new zlib stuff with that has just come out in Node Core API.
You can make use of this in your express server by adding a dependency to connect 2.0 in your package.json file:
{
...
dependencies: {
"connect" : "2.x",
"express" : "2.x",
// etc..
}
}
And then apply the following logic into your express app configuration:
// Create static file server with gzip support
var app = express.createServer(express.logger());
app.use(connect.compress());
app.use(express.static(__dirname + '/public'));
app.listen(80);
Please note that this stuff is still pretty new and while I could get it to work locally, my Heroku cloud application complained about the dependency on Compress 2.x during the pre-commit hook when deploying via git:
-----> Heroku receiving push
-----> Node.js app detected
-----> Resolving engine versions
Using Node.js version: 0.4.7
Using npm version: 1.0.106
-----> Fetching Node.js binaries
-----> Vendoring node into slug
-----> Installing dependencies with npm
npm ERR! Error: No compatible version found: connect#'>=2.0.0- <3.0.0-'
As you can see, they're still using an old version of node (0.4.7).
UPDATE:
Actually, I could get Heroku to deploy this by adding the corresponding engines section in the package.json:
{
...
"engines": {
"node": ">= 0.6.0 < 0.7.0"
}
}
And these are the results when using a http compression tester:
UPDATE June 2014
Hiya, if you are reading this now. Dont forget that the stuff above is only relevant to Express 2.0.
Express 3.0 and 4.0 use different syntax for enabling http compression, see post by gasolin just below.

I have also searched npm and found for example:
https://github.com/tomgallacher/gzippo
gzippo pronounced g-zippo is a gzip
middleware for Connect using Compress
for better performance.
Gzippo has recently been developed(2 days ago) which I think is a good thing. I can't tell you about production usage. You should test/benchmark it yourself. I would also probably use a CDN for a live site or Nginx to host my static files instead of some nodejs module.

Connect will support the new zlib stuff in Node in the next release

If you've searched the npm you may have come across node-compress.
It shouldn't be too hard to inject it as middleware into express.

Related

'serve' is not recognized as an internal or external command to run react app

serve has been installed globally using npm install -g serve command and It works locally, but deploying to a Windows server gives the following error:
"serve" is not recognized as an internal or external command
How to fix this error? Also, what is the use of the server.js file in a react project, and how does it help with deployment?
npm serve is installed globally please click here to see the image
I know that running npx serve -s build should work. I had the same problem as you. The npx command works for me. If you have npx problems, check your version of nodejs. I'm running 10.16.2 (so that we're on the same page). https://www.npmjs.com/package/serve
The rest of your question is relative to the rest of your set up. I don't have a server.js file of my own (there are some node_module server.js files, is that what you mean)?
As I understand a create-react-app, npm run start will allow you to run your application locally. I don't need serve -s build for that.
I used an amplify create react app. For an amplify application, I just run amplify publish and my application's build directory is sent to an S3 bucket for me. If you don't have that configuration, and you want the quick and dirty answer... just take the contents of your build directory in your react application and drop those files on your web server. That should get you 90% of the way there (mind the default page that renders).
Serving React Files
Basic Exapmle:-
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(9000);
For your base path in the domain serve the index.html you fetched from your build process.
If you need more info :- https://create-react-app.dev/docs/deployment

create-react-app shows an error of "You need to enable JavaScript to run this app."

I use express for my API. I have a folder named app and another folder named server. app is the client app, using create-react-app as boilerplate, while server is express.js app for the API.
in the app.js file of the server, I wrote
app.get("*", function (req, res) {
res.sendFile(path.resolve(__dirname, '../app/build/index.html'));
})
But then when I call any API endpoint, I get
You need to enable JavaScript to run this app.
in the response. I'm confused; what's wrong?
In the build directory you have more files that just index.html. You also have build/js/main.buildNumber.js and build/css/main.buildNumber.css.
So when your frontend makes a request to https://yourdomain.com/css/main.buildNumber.js, it incorrectly returns index.html not main.js.
Instead, you should serve the contents of the build folder statically with express.static
app.use('/', express.static(__dirname + '/'));
Or you can look into the "serve" node module to host your app. This will work nicely with react-router. npm i -g serve then cd build then serve . --single -p 5000. This will serve your app on port 5000 (http://localhost:5000).
For me, the issue was that I was mixing styles for functional and class based components. Double check that you don't have any dangling this keywords, or perhaps some missing of the same.

Error Building on Heroku - Isomorphic App

When trying to deploy my app on Heroku, I'm getting the following errors:
Cannot GET /
NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).
Server is being run outside of live development mode, meaning it will only serve the compiled application bundle in ~/dist. Generally you do not need an application server for this and can instead use a web server such as nginx to serve your static files. See the "deployment" section in the README for more information on deployment strategies.
I understand that I should be running it "live", rather than from the localhost, so I set the following settings via the CLI:
heroku config:set NODE_ENV=production
heroku config:set NODE_PATH=./src
heroku config:set NPM_CONFIG_PRODUCTION=false
What else should I check for. The app is a clone of this boiler: https://github.com/davezuko/react-redux-starter-kit
My Heroku link is: https://hidden-temple-43853.herokuapp.com/
Assuming you don't use a Procfile to tell Heroku how to launch your app, the npm start script of package.json will be used instead.
Are you running the npm run deploy with NODE_ENV=production before deploying to Heroku?
Have a look at this issue where a fix was suggessted on deploying to Heroku (copied below).
// ...
// ------------------------------------
// Apply Webpack HMR Middleware
// ------------------------------------
if (config.env === 'development') {
const webpackDevMiddleware = require('./middleware/webpack-dev').default
const webpackHMRMiddleware = require('./middleware/webpack-hmr').default
const compiler = webpack(webpackConfig)
// Enable webpack-dev and webpack-hot middleware
const { publicPath } = webpackConfig.output
app.use(webpackDevMiddleware(compiler, publicPath))
app.use(webpackHMRMiddleware(compiler))
// ...

How to use sql-injection package in Node.js application?

I want to prevent my app for SQL-Injection attack in Node.js,for that i am using sql-inection package of NPM.
My app.js File
var app = express();
var sqlinjection = require('sql-injection');
app.use(sqlinjection);
With this configuration i am directly sending request to server.
But with each request to the server the api does not send any response and gives no error or Warning.
I am using this Npm package sql injection npm js
Please guide me to how to use sql-injection in node.js and express.js project.
Thanks.
Please include following lines in your app.js file
app.use('/wordcloud',wordcloud);
app.use('/profanityfilter',profanityfilter);
app.use('/api/notification',notification);
app.use('/api/badge',badgecount);
app.use('/api/csrf',csrfRoute);
app.use(sqlinjection);
After requiring the npm package you must use this sql-injection package at the end of all your router.
and it will work fine.

Express + socket.io: socket.io client script is 404

This is driving me crazy... while I have a working version of Express + Socket.io, I can't seem to reproduce it with out-of-the-box NPM installs in a new project folder. Can anyone point out what I'm missing...? Here's my process:
I create a node_modules folder in my project directory (pwd), then do:
npm install express
npm install socket.io
Running those two commands puts the packages in my project's node_modules folder as expected. Now I set up my server with the following:
var express = require('express'),
server = express.createServer().use( express.static(__dirname+'./public') ).listen( 8080 ),
io = require('socket.io').listen(server);
My public folder contains static assets for my application. My public index HTML page includes a script tag for:
<script src="/socket.io/socket.io.js"></script>
Finally, I run my server script and go to the application in a web browser. My static public files are all served properly, however I get a 404 for /socket.io/socket.io.js. Now, I can swap in an express package from another old project and have this whole system work. Somehow that package instance is configured differently, but I can't figure out how to reproduce that. The Express website mentions something about installing dependencies, although running npm install -d doesn't seem to help (is there a specific pwd that I need to be in while running npm install -d?). I figure I must be missing something important about configuring a new Express instance after installing it with NPM.
Thanks for any and all insight!
Okay, so my example was actually an abbreviation of my code, and that example code does actually work. My real code with problems was a bit more cluttered, like so:
var server = express.createServer();
server
.use( server.router )
.use( express.static(__dirname+'/../public') )
.get('/api', function(req, res) {
res.write('API');
});
server.listen(8080);
var io = require('socket.io').listen(server);
I fixed the above code by doing the following:
server = server.listen(8080);
Apparently the listen command wraps the server object with some additional functionality. My originally posted shorthand example actually does work because listen is chained onto the final return into the server variable. Interesting little nuance.

Categories