当我尝试访问快递路线时未找到404错误

时间:2018-07-17 13:30:07

标签: javascript angularjs node.js express angular-ui-router

我正在运行带有Express js和mongdb服务器的cli cli生成的应用程序。运行npm start之后,我可以访问路由到我首页的http://localhost:3000,导航栏上的其他链接可以很好地控制路由,例如{{ 3}},这是通过单击黑板按钮加载的。但是,当我尝试输入http://localhost:3000/blackboardhttp://localhost:3000/blackboard时,它会显示为Not Found。 当我尝试重新加载页面时,它也以未找到响应。 这是我的 app.routing 文件

import { NgModule } from '@angular/core';
import { RouterModule,Routes} from '@angular/router';
import { MenuComponent } from './menu.component';
import { HomeComponent } from './home/home.component';
import { BlackboardComponent } from './blackboard/blackboard.component';
import { AboutComponent } from './about/about.component';
import { FooterComponent } from './footer/footer.component';
import { BookComponent } from "./book/book.component";
import { BookDetailComponent } from "./book-detail/book-detail.component";
import {BookCreateComponent} from "./book-create/book-create.component";
import {BookEditComponent} from "./book-edit/book-edit.component";


const routes: Routes = [

    { path: '', redirectTo: '/home', pathMatch: 'full' },
    {path: 'home', component: HomeComponent},
    { path: 'blackboard', component: BlackboardComponent },
    { path: 'about', component: AboutComponent },
    {  path: 'books',component: BookComponent},
    {  path: 'book-details/:id', component: BookDetailComponent},
    {path: 'book-create',component: BookCreateComponent},
    { path: 'book-edit/:id', component: BookEditComponent},
    ];


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

我通过bin / www运行服务器

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('mean-angular6:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}



function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

这是我的app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mean-angular6')
  .then(() =>  console.log('connection succesful'))
  .catch((err) => console.error(err));

var apiRouter = require('./routes/book');

var app = express();

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'dist/mean-angular6')));
app.use('/', express.static(path.join(__dirname, 'dist/mean-angular6')));
app.use('/api', apiRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.send(err.status);
});

module.exports = app;

2 个答案:

答案 0 :(得分:0)

在服务器端代码中未提及“ blackboard”,没有服务器端路由与其匹配,因此您得到404。在这种情况下,您需要添加一条路由来呈现主页,然后Angular可以呈现客户端黑板路线。

答案 1 :(得分:0)

您是否用邮递员或curl测试过api?我们无法在代码中识别您的api路线。在前端,您不能简单地重新加载页面并获取角度路线路径,但是可以在路由器模块中导入一个配置对象以使用哈希:

RouterModule.forRoot(routes, {useHash: true})

这里是文档https://angular.io/api/router/RouterModule