I have an express server, and I would like to provide route handling to manage an entire directory.
root
/html
/css
/js
/app.js
/images
/index.html
/some-other.html
/server.js
Do I need to serve every file individually using
app.get('/', (req, res) => {
res.send(htmlPath + '/index.html');
});
and adjust it for each .html file I am serving, or can I assign the /path-to-file as a variable and concatenate the variable into the htmlPath.
Additionally, is the following correct syntax to define the path/directory name
var htmlPath = path.join(__dirname, "html/");
app.use(express.static("html"));
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, "/html")));
I am relatively new and have been following various other questions and answers to get to where I am now.
according to SOLID principle and MCV structure, I suggest you to handle each part of your directory in an individual file.
root
/app
/statics
/images
/css
/views
/index.html
/some-other.html
/routes
/statics.js
/views.js
/index.js
/app.js
/server.js
so with this structure you have a routes directory which handle all the routes.
in the index.js(Routes) you send each request to the correct route file:
const staticsRouter = require('./statics');
const viewsRouter = require('./views');
module.exports = (app) => {
app.use('/statics',staticsRouterr);
app.use('/views',viewsRouter);
}
and in router files response to each request.
Related
I am having an issue with some custom routing code, it all works fine and is in sync with the client-side view routing I do, but as soon as I have a subpage, it doesn't route my static files correctly.
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
Rather than giving me a file from the root directory, it'll serve it as if it were from the subfolder.
Example: i go to http://localhost/sign-up, and files loading in my index file from /scripts are loaded, but if i go to http://localhost/sign-up/2, it'll attempt to load the script from /sign-up/scripts
const express = require('express');
const path = require('path');
const app = express();
app.use('/views', express.static(path.resolve(__dirname, 'frontend', 'views')));
app.use('/styles', express.static(path.resolve(__dirname, 'frontend', 'styles')));
app.use('/scripts', express.static(path.resolve(__dirname, 'frontend', 'scripts')));
app.use('/media', express.static(path.resolve(__dirname, 'frontend', 'media')));
app.get('/*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'frontend', 'newSite.html'));
});
app.listen(process.env.PORT || 1234, () => console.log('Server is now running...'));
To manage this I have been following these tutorials by DCODE on youtube, but I can't see anything amiss:
https://www.youtube.com/watch?v=6BozpmSjk-Y
https://youtu.be/OstALBk-jTc
Resources loaded in the sign up folder should use URLs beginning with a '/' character, to make them relative to the site root, e.g.
src="/scripts/modulefile.js"
href="/css/stylesheet.css"
href="/media/image.png"
and not urls relative to the signup folder - which they will be if the leading '/' is omitted.
You don't need multiple routes to serve your static contents, and the static method of express do such kind of tasks for you:
// If your 'public' or 'static' directory is one of root directories
app.use(express.static(process.cwd() + '/public'));
// so all these requests will be served:
// -> /public/styles/custom.css
// -> /public/scripts/pollyfils.js
// -> /public/media/logo.png
I am trying to use static files in my project, but when the user makes a get request to my "/" landing page. I want to send non-static things like a json. But for some reason it just automatically sends my index.html in my static file.
const express = require("express");
const app = express();
app.use(express.static("public"));
app.get("/", (req, res) => { //This sends the user to the index.html even tho i want to send 123
res.send("123");
});
app.listen("3000");
As your code is written, the express.static() middleware looks at the / request, finds an index.html file in the public directory and serves that as the response for the / request. This is a feature of express.static() that is causing a conflict for you with your custom route for "/".
You have at least four choices for a solution here:
You can specify an option for express.static() to disable the index.html feature so it will avoid the "/" route and pass control on to your subsequent route handlers.
You can move the express.static() middleware AFTER your app.get("/", ...) route so that your custom route gets first dibs at the request.
You can remove the index.html file from your public directory so express.static() won't find it. Using a template system for all your HTML files that locates all HTML template files in some other directory that express.static() can't see (such as a views directory) would cause this to happen too or just moving it to some private directory and using it from your code from the private directory would work too.
Give ALL your static resources a common path prefix so there is never a URL conflict between static resources and custom route handlers.
The first option (disable index.html feature in express.static()) would look like this:
const express = require("express");
const app = express();
// disable index.html feature
app.use(express.static("public"), {index: false});
app.get("/", (req, res) => {
res.send("123");
});
app.listen("3000");
The second option (change the order of route definitions/middleware) would look like this:
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("123");
});
// put this after custom route definitions so they take precendence
app.use(express.static("public"));
app.listen("3000");
The fourth option (give all static resources a common path prefix) would look like this:
const express = require("express");
const app = express();
app.use(express.static("/static", "public"));
app.get("/", (req, res) => {
res.send("123");
});
app.listen("3000");
With this fourth option, you'd then have to prefix all your static URLs with the static prefix (which you can make anything you want) and thus "/" would never be a match for express.static().
Is there any particular reason that you have an index.html file inside your public directory and are serving something else? The convention is serve static assets such as CSS, JS, images etc. from your public folder, and HTML / templates in folder which would be a sibling of public such as views.
The browser will always default to rendering an index.html file if it is found in the root of the public directory. Move the HTML file to a separate folder and the route should work
app.use is a middleware, so whenever you're running a app, it always first go through the middleware, Try the below code
const express = require("express");
const app = express();
app.get("/", (req, res) => { //This sends the user to the index.html even tho i want to send 123
res.send("123");
});
app.use(express.static("public"));
app.listen("3000");
I have a folder called "Public" which contains an index.html file a long with some JavaScript and library files. When someone tries to access the products path (mydomain.com/products) I want to display that index.html file, but the client also needs to receive all the JavaScript and libraries. Here is the code for how I initially handle the HTTP request.
const express = require('express')
const app = express()
const bodyParse = require('body-parser')
const productRoutes = require('./api/routes/products')
const orderRoutes = require('./api/routes/orders')
app.use(bodyParse.urlencoded({extended: false}))
app.use(bodyParse.json())
// Routes which handle requests
app.use('/products', productRoutes)
app.use('/orders', orderRoutes)
In the products.js file, I continue the routing like this:
const express = require('express')
const router = express.Router()
router.get('/', (req, res, next) => {
/*res.status(200).json({
message: 'Handling GET requests to /products'
})*/
res.status(200).render("../../public")
})
The code I've commented out works perfectly fine, but I'm struggling to respond with the "public" folder page. I can't remember everything I've tried, but using .render or .sendFile on the "public" directory has not worked for me.
When I try to access the /products route, I'm hit with an empty error message. As it fails to return anything in the /products route, in continues down the file to an error handler. The error message is empty.
Any ideas on how to display the page with all the folder contents would be great!
Try: app.use('/products', express.static('public'))
This makes your "public" directory viewable from the /products route.
express.static() docs
You must config path for express static by :
//app.js | server.js
app.use(express.static(__dirname + '/public'));
Then, in example you have a file as : /public/you.html
In your app, you can use that file as path /you.html
And the same with all files type *.js, *.css,...
Fix error cannot view error
Run cmd npm install ejs
Att to app.js:
app.set('view engine', 'ejs');
After that, you create 1 file error.ejs at folder views :
//error.ejs
<%=error%>
Goodluck
I have a sails app and the routes,static assets associated with the app are served from root and it works fine. I would like to add a express middleware so i could serve the routes and static assets in a specific path.
In order to serve the static assets i used the below inside the customMiddleware function in config/http.js,
app.use('/my/new/path', express.static(path.join(__dirname, '../client', 'dist')));
With the above in place i was able to load the static files both from the root as well as from the /my/new/path.
Now when it comes to route i'm not sure how to handle the sails route to load via express middleware using app.use, for example the home route defaults to '/' in my config/routes-client.js, instead of altering the route there i would like to use something like below which we normally use for a typical node/express app,
app.use('/my/new/path', routes); --> where routes is my server routes
Is there a way to add a express middleware to serve sails route on a specific path?
I'm not sure why you'd want to have a prefix added automatically to your custom routes rather than just adding the prefix directly to the routes in the config/routes.js (or config-routes-client.js) file, but here goes:
// config/http.js
middleware: {
reroute: function (req, res, next) {
// Set the prefix you want for routes.
var prefix = '/foo/bar';
// Create a regex that looks for the prefix in the requested URL.
var regex = new RegExp('^' + prefix + '(/|$)');
// If the prefix is found, replace it with /
if (req.url.match(regex)) {
req.url = req.url.replace(regex, '/');
}
// Continue processing the request.
return next();
}
}
Make sure you add reroute to the middleware.order array in config/http.js. Incidentally, this will take care of static files as well.
This is how I do it...
Inside http.js
var express = require('express');
module.exports.http = {
customMiddleware: function (app) {
app.use('/docs', express.static(path.join(sails.config.appPath, '/docs')));
},
... rest of the http.js code...
}
Of course in this example im serving docs folder from root of my app on route /docs... You addapt to your logic...
Is there a way to access the root public directory without using ../../../ something like this. The below code works fine, but just thought the way i am accessing my public folder is bit messier.
router.get('/', function(req, res) {
res.sendFile('index.html', { root: path.join(__dirname, '../../public') });
});
Application structure:
/app
--node-modules
--public
--app //contains all angularjs related files
--assets //contains images and js libraries
--index.html
--server
--routes
--index.js //this holds all the route related code
On way is to use app-root-path. It is a module that helps you access your application's root path from anywhere in the application without resorting to relative paths.
var appRoot = require('app-root-path');
router.get('/', function(req, res) {
res.sendFile('index.html', { root: path.join(__dirname, appRoot + '/public') });
});