I have tried several ways of serving up a static directory. Here is a simple way I am doing this.
var app = express();
app.all('*', express.static('./public'));
module.exports = app;
// run the server
http.createServer(app).listen(8080);
I have several other configurations like:
app.use('/', express.static('./public'));
There is an index.html file in the public directory that gets served up fine. The only thing in the HTML file is a request for a JavaScript file. When that request gets made, express throws a 301 redirect, and adds a trailing slash.
Here is the HTML:
<script type="text/javascript" src="/dist/bundle.js"></script>
Here is the network request.
Any help is appreciated.
A 301 redirect is the best way to resolve duplicate content issues caused by trailing slashes. If you're just fixing one page, you'd redirect the duplicate copy to the version that matches your chosen URL structure. Most trailing slash issues however, affect many pages across a website.
A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash. However, these are guidelines and not requirements.
Express provides methods to specify what function is called for a particular HTTP verb ( GET , POST , SET , etc.) and URL pattern ("Route"), and methods to specify what template ("view") engine is used, where template files are located, and what template to use to render a response.
Lesser Development Time: Express uses Javascript for both backend and frontend development. Thus, developers can generate codes faster without the need to learn a new language.
You can enable strict route mode and use a route middleware (https://stackoverflow.com/a/15773824/781251)
http://expressjs.com/en/api.html
const router = express.Router({ strict: true })
This means the asset can't be found. It renders a 404 response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With