I have created an express angular app which works fine but if I am at a URL say http://localhost:4007/login and hit refresh it gets an error tried many things to correct it nothing works.
My server snippet code is as follow.
app.use(express.static(path.join(__dirname, '../client/dist/bringclean')));;
app.get('/*', function (req, res){
res.redirect('/');
});
app.listen(4007, function () {
console.log('BringClean app is running on port 4007');
});
Found a way to solve the problem.
const routes = require('express').Router();
routes.get('*', function (req, res) {
res.sendFile(path.join(__dirname + '/../client/dist/projectname/index.html'));
});
Related
I am using Express with Node and then building a React app with this which is single page, except one static HTML file.
I have this working locally, so when I go to localhost:3000/static-file.html, I can see the file. But this doesn't work in production.
I can't even hit any of the test console.log statements, so I know that nothing is being hit when requesting static-file.html. Please see below part of my app.js file:
if (process.env.NODE_ENV === "production") {
app.get('/static-file.html', function (req, res) {
console.log('test1');
});
app.get('static-file.html', function (req, res) {
console.log('test2');
});
app.get('static-file', function (req, res) {
console.log('test3');
});
app.get('/static-file', function (req, res) {
console.log('test4');
});
app.use(express.static("client/build"));
}
When I go to production-site.com/static-file.html - I just see the index still, unlike with localhost.
Can anybody help me on that? Thanks so much.
Try something like this:
const express = require('express');
const app = express();
const path = require('path');
// the __dirname is the current directory from where the script is running
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/build/index.html'))
})
/* Here build is the production build directory and index.html is the main html page to show */
I have the following express server setup:
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
console.log('/*');
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get('/test', function (req, res) {
console.log('test');
//res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(9000);
Everytime I hit localhost:9000/test, it still take to the route '/'.
I tried placing the app.get code before the static declaration but I don't think that really makes any difference. Any idea why this is happening?
Your existing code is fine. It should work.
I believe you had your index route as app.get('/*', function (req, res){ instead of app.get('/', function (req, res) { before and that's why it was capturing all requests.
In the /test route handler you have to terminate the request-response cycle by using res.end(), something like this:
app.get('/test', function (req, res) {
console.log('test');
res.end();
});
Otherwise the page will never refresh showing the first page (route /)
Hope this helps
I have trouble implementing route mounting in express.js 4.13.3.
When I first install it, by default created in the app.js
var users = require('./routes/users');//get the "users" route
app.use('/users', users);//mount to "/users"
and the users.js route is like
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.send('respond with a resource');
});
module.exports = router;
Does not need to define router.get('/users'... because mounting took care of that in the app.js file.
But
When I try to do the same thing
in app.js I set
var upload = require('./routes/upload');
app.get('/upload', upload);//mounting (?)
the upload.js route is
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.render('upload', {title: 'Photo upload'});
});
module.exports = router;
When I access localhost/users I get 404 error. The only way to fix this , is to define in the upload.js router, this router.get('/upload' instead of this router.get('/'. But that would not be mounting.
The difference I see is that the default code uses app.use('/users', users); and my code uses app.get('/upload', upload);. Is the verb (use/get) the only difference that causes the 404? And if so, why? Or is it something else?
Thanks
You are totally correct that the problem is caused because these to functions work differently. Below are the official API specifications for the functions.
app.use is for mounting a middleware
app.get is for defining (only) one route for a HTTP GET request
This example shows a middleware function mounted on the /user/:id path. The function is executed for any type of HTTP request on the /user/:id path.
app.use('/user/:id', function (req, res, next) {
console.log('Request Type:', req.method);
next();
});
This example shows a route and its handler function (middleware system). The function handles GET requests to the /user/:id path.
app.get('/user/:id', function (req, res, next) {
res.send('USER');
});
app.get("/test", function(req, res, next) {
res.sendFile(__dirname + '/test.html');
});
Pretty basic, yes? Running this server on my Mac this works fine. Running this server on a PC my browser receives "cannot GET" error. I don't think the issue is the permissions on the mp4 file, they seem to be ok.
Sorry guys, not sure what I did differently but it is working now!
For the record, here is the full app:
var express = require('express');
var app = express();
app.use(express.static('dist'));
app.get("/demo", function(req, res, next) {
res.setHeader('Content-Type', 'video/mp4');
res.sendFile(__dirname + '/demo.mp4');
});
var server = app.listen(80, function () {});
I have an app with following code for routing:
var router = express.Router();
router.post('/routepath', function(req, res) {});
Now I have to put routing code in different files so I tried to use this approach, but it is not working perhaps because instead of express.Router() it uses:
app.post("/routepath", function (req, res) {});
How can I put routing in different files using express.Router()?
Why app.get, app.post, app.delete, etc, are not working in app.js after using express.Router() in them?
Here's a simple example:
// myroutes.js
var router = require('express').Router();
router.get('/', function(req, res) {
res.send('Hello from the custom router!');
});
module.exports = router;
// main.js
var app = require('express')();
app.use('/routepath', require('./myroutes'));
app.get('/', function(req, res) {
res.send('Hello from the root path!');
});
Here, app.use() is mounting the Router instance at /routepath, so that any routes added to the Router instance will be relative to /routepath.