I am hosting a server with nodejs, and have created a login, signup and menu. My login looks like this:
While my signup looks like this:
This is a snippet of my code:
const app = express();
app.use(express.static(path.join(__dirname + '/views')));
app.get('/', checkAuthenticated, (req, res) => {
res.render('index');
});
app.get('/users/signup', checkAuthenticated, (req, res) => {
res.render('signup');
});
Why will the CSS not load for signup, but will for login?
You are likely using relative paths instead of absolute paths. try replacing your 'views/view.css' with '/views/view.css' in your html file.
Related
I am trying to use express to render a few different html files from my public folder. These are all static files. I also want to render a 404 page if a route is an invalid route is called. Here is my code.
const express = require("express")
const app = express()
app.use(express.static("public"))
app.get("/", (req, res) => {
res.render("index")
})
app.get("/about", (req, res) => {
res.render("about")
})
app.get("/contact-me", (req, res) => {
res.render("contact-me")
})
app.get("*", (req, res) => {
res.status(404).send("404 for all pages not defined in routes")
})
app.listen(8080)
The first route to render index.html works and the 404 status works, but all the other routes give me an error of "No default engine was specified and no extension provided." I tried added an ejs view engine, but the code still doesn't work. All html files are named properly and live in the "public" folder. Any help on this would be amazing! Thanks much!
You need to use handlebars for handling it. To see an example check this repo
I am trying to create an SPA using Express with following code
var express = require('express');
const path = require('path');
var app = express();
app.use('/assets', express.static(path.resolve(__dirname, 'www', 'assets')));
app.get('/*', (req, res)=>{
res.sendFile(path.resolve('www', 'index.html'));
});
var server = app.listen(3000, function(){});
This code works good but the problem is this responds with my index.html even when a file is not found in my assets folder. I want it to respond with error of 404 Not Found if some url is not present in assets folder
I tried using this code after line app.use('/assets'...
app.use(function (req, res, next) {
res.status(404).send("404 Not Found");
});
but not working
Issue is with
app.get('/*', (req, res)=>{
res.sendFile(path.resolve('www', 'index.html'));
});
Instead, use
app.get('/', (req, res)=>{
res.sendFile(path.resolve('www', 'index.html'));
});
So, I finally got the way to solve it
app.get('/*', (req, res)=>{
if(req.path.includes('/assets'))
{
fs.access(req.path, (err) => {
if(err)
{
res.status(404).send("Sorry can't find that!");
return;
}
});
}
res.sendFile(path.resolve('www', 'index.html'));
});
In the code, I have told to check if the request is about a file located in assets folder then check if the file exists, if does not exists then send 404 error with message and just return, otherwise do nothing but return the file.
I need to refresh the page in my small Node and Express app as soon as the user connects. I have tried res.redirect('index.html') in the second snippet, but it throws an error when I try to connect to the root URL.
The following code is run when the user connects to the root URL:
app.get('/', (req, res) => {
res.redirect('/index.html')
app.use(express.static(path.join(__dirname, 'public')))
});
and I detect when the user connects with:
io.on('connection', (socket) => {}
Thanks in advance!
app.get('/', (req, res) => {
// res.redirect('back');
// OR
res.redirect(req.get('referer'));
app.use(express.static(path.join(__dirname, 'public')))
});
I have a root directory containing two folders, that all have more folders within, I'm trying to reach the specified file and render it:
frontpage [FOLDER]
- server.js
login_register [FOLDER]
routes [Folder]
- index.js
- users.js
views
- movie_main_page.ejs
How can I take a file (server.js) from frontpage folder and use it in the login_register folder? I'm also trying to render this file as it will now serve as the main page instead of the dummy landing page after logging in (movie_main_page.ejs).
This is the code I use for rendering the landing page:
index.js
const express = require('express');
const router = express.Router();
//Main page of login/register
router.get('/', (req, res) => res.render('welcome'));
//Main page of movie site goes here
router.get('/movie_main_page', (req, res) => res.render('movie_main_page'));
module.exports = router;
users.js
router.post('/login', (req, res, next) => {
passport.authenticate('local', {
successRedirect: '/movie_main_page'
})(req, res, next);
});
module.exports = router;
I haven't posted the full code from the files because the system works, it renders the movie_main_page fine after logging in, but how can I render another file now that the dummy one works?
Thanks!
I have an Angular app running alongside a NodeJS server.
This code in my server.js file:
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
require('./server/routes/routes')(app, passport);
Seems to be preventing any GET requests to my api, for example:
module.exports = function (app, passport) {
app.get('/api/profile', isLoggedIn, function (req, res) {
res.status(200).json(req.user);
});
When the exported /api/profile handler is changed to a post the request works.
Should the route not overwrite the initial handler? How to achieve this? Can I serve the app for all routes excluding beginning with '/api'?
Move the app.get('*') so it becomes the last route that gets declared:
require('./server/routes/routes')(app, passport);
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
Express checks route handlers in order of their declaration, not in order of their specificity (how well they match a particular request).