Why specify the path in app.use? - node.js

//app.js
var app = require('express')();
app.use('/test', require('./test'));
//test/index.js
var router = require('express').Router();
router.get('/test', function (req, res) {
res.status(200).send("success");
});
module.exports = routes;
Why does the path need to be specified in app.use and router.get? If I simply put app.use('/', require('./test')); instead, it seems to work just fine still.

If you change this:
app.use('/test', require('./test'));
to this:
app.use('/', require('./test'));
then you will have the same functionality as before of using the middleware exported by the ./test module on the routes that start with /test so you will experience that everything will work the same, but that middleware will also handle every other route not necessarily starting with /test which, depending on what it does and how it works, may or may not be what you want.
By using some path in the app.use() call you restrict the middleware that you're loading to that path only. When you use / then it's like saying "every path" because every path starts with the slash - even paths that are requested for URLs that doesn't include the slash are still requested with slash, using e.g. with HTTP/1.1 something like:
GET / HTTP/1.1
Host: localhost

With specifying router.get('/test', function (req, res) your handler method will handle any request thats ends in /test. Depends on where the router is use().
app.use(withPath, [callback...]
That mount your middleware functions tests at the speficied path /test
So your middlewares test will be execute when the base request url path match.

Related

can express.static find your mounting file

I have seen some code online and was wondering how the index.HTML was getting rendered when we are not using any app.get()
the public file structure follow like this:
css(folder),
js(folder),
chat.html,
index.html,
const path = require('path');
const express = require('express');
const app = express();
// Set static folder
app.use(express.static(path.join(__dirname, 'public')));
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
When an incoming request matches a directory name that express.static() is configured for, then express.static() will look for an index.html file in that directory and, if found, will serve it.
You can see this feature mentioned here in the doc, in the index option you can pass express.static() that allows you to specify the name of the index.html file to look for (default value is index.html) or you can set it to false to disable the feature.
So, in your code example, with this:
app.use(express.static(path.join(__dirname, 'public')));
an incoming http request for / will look for public/index.html and, if found, will serve it automatically. Similarly a request for /foo will look first for /public/foo and if that is present and is a directory, then it will look for public/foo/index.html and, if found, will serve it.

Proxy all requests to server in React using createProxyMiddleware

So on my dev env, I have a React Server on localhost:3000 and a NodeJs server on localhost:5000.
I have used createProxyMiddleware in React to configure the requests. Here is my config:
const {createProxyMiddleware} = require('http-proxy-middleware');
module.exports = function (app) {
app.use(createProxyMiddleware('/auth/google', {target: 'http://localhost:5000'}));
app.use(createProxyMiddleware('/auth/currentUser', {target: 'http://localhost:5000'}));
app.use(createProxyMiddleware('/api/*', {target: 'http://localhost:5000'}));
}
All of these routes work without any problems. For example I have a route like localhost:3000/api/list which gets routed to localhost:5000/api/list.
But now I have a route like localhost:3000/api/list/3132133ffdvf. The random string is some id. This above route does not get proxied. It gives me a 404.
Why is this happening? It shouldn't happen since I have created a proxy for /api/*. Can someone help me out?
The wildcard (*) only matches one directory level. This means that it matches /api/list but not /api/list/id, since the latter has two levels.
Either remove the wildcard, or use a globstar (**):
app.use(createProxyMiddleware('/api', {target: 'http://localhost:5000'}));
or
app.use(createProxyMiddleware('/api/**', {target: 'http://localhost:5000'}));

Express 4 route won't hit

const express = require('express');
const path = require('path');
const app = express();
app.listen(9000);
app.get('/test', function(req, res) {
console.log("not being hit");
res.send(200);
});
app.use(express.static(path.join(__dirname, 'build')));
app.get('/*', function (req, res) {
console.log("always hits");
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
This seems like such a simple problem but my brain is starting to turn to mush.
Here's the details:
I have run build on a react app and the index.html file resides in the build folder, and I want this served via express.
I want express to prioritize /test first, and if it's not /test, then I want it to serve the index.html file in the build folder.
If you go to /test, it is skipped and always hits the /* route. If you remove the wild card and use / instead, neither routes will hit if you go to / or /test in the browser. However, index.html is still served and it looks like that's because of the static middleware.
Everything I have read suggests that order in express is important, but I feel like I'm losing my damn mind and I'm starting to slowly descend into madness.
Thanks in advance.

serveStatic() unable to locate a file

I am new to node.js, and I am trying to use the serveStatic function to locate a specific file.
When I specify the exact path in the argument it doesn't work. However it works only when I specify the directory only , and name the file index.html inside that directory.
Any idea on how I can use to it locate a specific file? Any help would be really appreciated from you guys thanks!
Below is my code
var connect = require('connect'),
http = require('http'),
serveStatic = require('serve-static');
var app = connect();
// app.use(serveStatic('public')); Works
app.use(serveStatic('public/test.html'));// doesn't work
app.use(function(req, res){
});
http.createServer(app).listen(3000);
serve-static is meant to serve a whole directory (with all sub-directories, if any). You can't use it to serve just one single file.
What you could do with serve-static is to set a default file to be sent when user requests a root of your directory (by default it's an index.html file):
app.use(serveStatic('public', {index: 'test.html'}));
But if you really want to send just a single file, then is's better to use this answer:
app.use(function(req, res) {
res.sendfile('test.html', {
root: __dirname + '/public/'
});
});
Though, the best possible solution is to read this file once and cache it. In this case there will be no need to access your storage device each time somebody is requesting this file:
var html_data = require('fs').readFileSync('./public/test.html');
app.use(function(req, res) {
res.send(html_data);
});

Node crash using express and static middleware when URL contains a trailing backslash

I have a simple Express server that is serving up some static files. Here is the server:
var express = require('express');
var app = express.createServer();
// Configuration
app.configure(function() {
app.use(express.bodyParser());
app.use(express.staticCache());
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
// 404
app.get('*', function(req, res) {
res.send('not found', 404);
});
app.listen(3000);
In my public directory I have a file called index.html. Firing up node app.js and then browsing to localhost:3000/index.html shows the static file as expected. Navigating to localhost:3000/ind or localhost:3000/ind\ shows the 404 page as expected.
However, navigating to localhost:3000/index.html\ (note the trailing backslash) crashes my node server with:
stream.js:105
throw er; // Unhandled stream error in pipe.
^
Error: ENOENT, no such file or directory '/home/bill/projects/app/public/index.html\'
Why is the node server crashing instead of just serving up the 404 page? I thought since the file does not exist, the static middleware would just skip it and pass the request on to the routes. I got around it by creating a custom middleware that returns 404 if a trailing backslash exists in the request URL, but I'd like to figure out if I'm missing something here. Thanks!
The reason for this behavior seems to be the difference in how fs.stat and fs.createReadStream handle trailing backslashes.
When the string 'path/to/public/index.html\\' is given to fs.stat in the static middleware, it is ignored (running stat index.html\ on the command line checks for a file named index.html, you'd have to run stat index.html\\ for index.html\). So fs.stat thinks the file was found because it thinks you're asking for index.html, and doesn't call the next middleware handler.
Later, that string is passed to fs.createReadStream which thinks it's looking for index.html\. It doesn't find that file and throws said error.
Since the functions treat the backslash differently, you can't really do anything but use some middleware to filter out those requests.

Resources