How to fix Nodejs connect cannot find static error?

Sometimes, we want to fix Nodejs connect cannot find static error.

In this article, we’ll look at how to fix Nodejs connect cannot find static error.

How to fix Nodejs connect cannot find static error?

To fix Nodejs connect cannot find static error, we should install the serve-static package.

We install it by running

npm i serve-static

Then we use it by writing

const connect = require('connect')
const serveStatic = require('serve-static');

const app = connect();

app.use(serveStatic("../public"));
app.listen(5000);

to call app.use with the middleware returned by serveStatic called with the path to the static folder we want to serve.

Conclusion

To fix Nodejs connect cannot find static error, we should install the serve-static package.