最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting 404 page on page refresh using node and angular app - Stack Overflow

programmeradmin0浏览0评论

I am new in Angular. I tried to create a CRUD operation using Nodejs and Angular. I am using Nodejs and Express for backed and Angular for frontend.

When I navigate on page using routerLink its work fine, but when I navigate on the page and then I refresh the page it shows a page not found error.

I know what happened there, the route is not defined in the Express app so I refresh the page it shows the 404 error.

How do I make this work with Angular and Express routes?

Here is my code.

app.js ( Server )

const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
var app = express();


app.use(bodyParser.urlencoded({extended : false}));
app.use(bodyParser.json());

app.use(express.static(path.join(__dirname,'dist/MEAN-blog')));


app.get('/',(req,res) =>{
    res.sendFile(path.join(__dirname,'dist/MEAN-blog/index.html'));
});

app.post('/register',(req,res) =>{
    var u = new User(req.body);
    u.save();
    res.send(u).status(200);
})


var server  = http.createServer(app);

server.listen(8080,'0.0.0.0',() =>{
    console.log('Server is running on 8080');
})

Here is the routes of angular

const routes: Routes = [
    {
        path : '',
        ponent : PostsComponent
    },
    {
        path : 'users',
        ponent : UsersComponent
    },
    {
        path : 'users/:id',
        ponent : UsersComponent
    },
    {
        path : 'post/:id',
        ponent : PostDetailsComponent
    },
    {
        path : 'login',
        ponent : LoginComponent
    },
    {
        path : 'signup',
        ponent : SignupComponent
    },

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

I am using angular 5

how can i solve this issue ?

Thanks

I am new in Angular. I tried to create a CRUD operation using Nodejs and Angular. I am using Nodejs and Express for backed and Angular for frontend.

When I navigate on page using routerLink its work fine, but when I navigate on the page and then I refresh the page it shows a page not found error.

I know what happened there, the route is not defined in the Express app so I refresh the page it shows the 404 error.

How do I make this work with Angular and Express routes?

Here is my code.

app.js ( Server )

const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
var app = express();


app.use(bodyParser.urlencoded({extended : false}));
app.use(bodyParser.json());

app.use(express.static(path.join(__dirname,'dist/MEAN-blog')));


app.get('/',(req,res) =>{
    res.sendFile(path.join(__dirname,'dist/MEAN-blog/index.html'));
});

app.post('/register',(req,res) =>{
    var u = new User(req.body);
    u.save();
    res.send(u).status(200);
})


var server  = http.createServer(app);

server.listen(8080,'0.0.0.0',() =>{
    console.log('Server is running on 8080');
})

Here is the routes of angular

const routes: Routes = [
    {
        path : '',
        ponent : PostsComponent
    },
    {
        path : 'users',
        ponent : UsersComponent
    },
    {
        path : 'users/:id',
        ponent : UsersComponent
    },
    {
        path : 'post/:id',
        ponent : PostDetailsComponent
    },
    {
        path : 'login',
        ponent : LoginComponent
    },
    {
        path : 'signup',
        ponent : SignupComponent
    },

];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

I am using angular 5

how can i solve this issue ?

Thanks

Share Improve this question edited Feb 15, 2019 at 23:36 HDJEMAI 9,82048 gold badges76 silver badges98 bronze badges asked Feb 15, 2019 at 18:26 Kaushik MakwanaKaushik Makwana 2,57610 gold badges37 silver badges55 bronze badges 7
  • what is the default router path? – ABOS Commented Feb 15, 2019 at 18:28
  • you mean base path? – Kaushik Makwana Commented Feb 15, 2019 at 18:29
  • base path <base href="/"> – Kaushik Makwana Commented Feb 15, 2019 at 18:30
  • does your index.html have <router-outlet></router-outlet>? – dev-dan Commented Feb 15, 2019 at 18:30
  • I mean in the definition of routes. what is the path in browser after you refresh? – ABOS Commented Feb 15, 2019 at 18:31
 |  Show 2 more ments

1 Answer 1

Reset to default 7

Usually what I do is include all my api endpoints first, and then at the end add this

app.get('*',(req,res) =>{
    res.sendFile(path.join(__dirname,'dist/MEAN-blog/index.html'));
});

This way any endpoint that isn't /api will return index.html and angular routing will take over from there.

Another alternative is using SSR but its kind of a pain.

发布评论

评论列表(0)

  1. 暂无评论