Node.js 没有击中快速路由

Node.js 没有击中快速路由,node.js,angular,Node.js,Angular,notification.service.ts中的My Angular HTTP GET请求indside clearNotifications()未命中Express Route routes/notifications.js。我正在从名为app.component.ts的组件调用clearNotifications()。我用的是角度7+ 路由/通知.js const router = require('express').Router(); //Additional modules //

notification.service.ts中的My Angular HTTP GET请求indside clearNotifications()未命中Express Route routes/notifications.js。我正在从名为app.component.ts的组件调用clearNotifications()。我用的是角度7+

路由/通知.js

const router = require('express').Router();

//Additional modules
// const db = require('../config/database');
// const notificationModel = require('../models/notifications');

//Test connection
// db.authenticate().then(() => {
//     console.log('Connection has been established successfully.');
// }).catch(err => {
//     console.error('Unable to connect to the database:', err);
// });

//Clear all notifications
router.get('/clear', (req, res, next) => {
    console.log('clear');
    // notificationModel.destroy({});
});
import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class NotificationService {

  uri = 'http://localhost:5000'; 
  private socket = io(this.uri);

  constructor(private http: HttpClient) { }

  getNotification() {
    let observable = new Observable<{ string: String, number: String }>(observer => {
      this.socket.on('notification', (data) => {
        observer.next(data);
      });
      // return () => { this.socket.disconnect(); }
    })
    return observable;
  }

  clearNotifications() {
    return this.http.get(`${this.uri}/notifications/clear`);
  }
}
import { Component } from '@angular/core';
import { NotificationService } from './notification.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [NotificationService]
})
export class AppComponent {
  title = 'client';

  string: String;
  number: String;
  notificationArray: Array<{ string: String, number: String }> = [];

  constructor(private notificationService: NotificationService) {
    this.notificationService.getNotification().subscribe(data => {
      this.notificationArray.push(data);
    });
  }

  clearNotifications() {
    this.notificationArray = [];
    this.notificationService.clearNotifications();
  }
}
module.exports=路由器

通知.service.ts

const router = require('express').Router();

//Additional modules
// const db = require('../config/database');
// const notificationModel = require('../models/notifications');

//Test connection
// db.authenticate().then(() => {
//     console.log('Connection has been established successfully.');
// }).catch(err => {
//     console.error('Unable to connect to the database:', err);
// });

//Clear all notifications
router.get('/clear', (req, res, next) => {
    console.log('clear');
    // notificationModel.destroy({});
});
import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class NotificationService {

  uri = 'http://localhost:5000'; 
  private socket = io(this.uri);

  constructor(private http: HttpClient) { }

  getNotification() {
    let observable = new Observable<{ string: String, number: String }>(observer => {
      this.socket.on('notification', (data) => {
        observer.next(data);
      });
      // return () => { this.socket.disconnect(); }
    })
    return observable;
  }

  clearNotifications() {
    return this.http.get(`${this.uri}/notifications/clear`);
  }
}
import { Component } from '@angular/core';
import { NotificationService } from './notification.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [NotificationService]
})
export class AppComponent {
  title = 'client';

  string: String;
  number: String;
  notificationArray: Array<{ string: String, number: String }> = [];

  constructor(private notificationService: NotificationService) {
    this.notificationService.getNotification().subscribe(data => {
      this.notificationArray.push(data);
    });
  }

  clearNotifications() {
    this.notificationArray = [];
    this.notificationService.clearNotifications();
  }
}
从'@angular/core'导入{Injectable};
从“socket.io客户端”导入*作为io;
从“rxjs”导入{Observable};
从'@angular/common/http'导入{HttpClient};
@注射的({
providedIn:'根'
})
出口类通知服务{
乌里http://localhost:5000'; 
私有套接字=io(this.uri);
构造函数(私有http:HttpClient){}
getNotification(){
让可观察=新可观察(观察者=>{
this.socket.on('通知',(数据)=>{
下一步(数据);
});
//return()=>{this.socket.disconnect();}
})
可观测收益;
}
clearNotifications(){
返回this.http.get(`${this.uri}/notifications/clear`);
}
}
应用程序组件.ts

const router = require('express').Router();

//Additional modules
// const db = require('../config/database');
// const notificationModel = require('../models/notifications');

//Test connection
// db.authenticate().then(() => {
//     console.log('Connection has been established successfully.');
// }).catch(err => {
//     console.error('Unable to connect to the database:', err);
// });

//Clear all notifications
router.get('/clear', (req, res, next) => {
    console.log('clear');
    // notificationModel.destroy({});
});
import { Injectable } from '@angular/core';
import * as io from 'socket.io-client';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class NotificationService {

  uri = 'http://localhost:5000'; 
  private socket = io(this.uri);

  constructor(private http: HttpClient) { }

  getNotification() {
    let observable = new Observable<{ string: String, number: String }>(observer => {
      this.socket.on('notification', (data) => {
        observer.next(data);
      });
      // return () => { this.socket.disconnect(); }
    })
    return observable;
  }

  clearNotifications() {
    return this.http.get(`${this.uri}/notifications/clear`);
  }
}
import { Component } from '@angular/core';
import { NotificationService } from './notification.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [NotificationService]
})
export class AppComponent {
  title = 'client';

  string: String;
  number: String;
  notificationArray: Array<{ string: String, number: String }> = [];

  constructor(private notificationService: NotificationService) {
    this.notificationService.getNotification().subscribe(data => {
      this.notificationArray.push(data);
    });
  }

  clearNotifications() {
    this.notificationArray = [];
    this.notificationService.clearNotifications();
  }
}
从'@angular/core'导入{Component};
从“./notification.service”导入{NotificationService};
@组成部分({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.css'],
提供者:[通知服务]
})
导出类AppComponent{
标题=‘客户’;
字符串:字符串;
编号:字符串;
notificationArray:Array=[];
构造函数(私有notificationService:notificationService){
this.notificationService.getNotification().subscribe(数据=>{
this.notificationArray.push(数据);
});
}
clearNotifications(){
this.notificationArray=[];
this.notificationService.clearNotifications();
}
}

angular
中,
http
请求返回
observable
,因此您需要
订阅
。如果没有任何可观察的
订阅方
,它将不会被执行。试一试

    clearNotifications() {
    return this.http.get(`${this.uri}/notifications/clear`)
               .subscribe(data => //your callback function,
                          error => // your error handler,
                          complete => // any after completion task);
  }

您应该这样做:检查

还要确保
订阅组件中的服务方法。如果不订阅,则不会执行
可观察对象

您在哪里呼叫
clearNotifications

订阅组件中的
clearNotifications
,这将起作用:
this.notificationService.clearNotifications().subscribe((数据)=>{..})

作为发布者,您可以创建一个可观察的实例来定义订阅服务器函数。这是消费者调用
subscribe()
方法时执行的函数。订户函数定义如何获取或生成要发布的值或消息


@不,我不是。但是,除了获得响应之外,它还重要吗?是的,订阅对于激活可观察对象和触发请求非常重要@xyz但我只关心clearNotifications(),你从哪里调用
clearNotifications
?@ShouryaSharma,你应该这样做
this.notificationService.clearNotifications().subscribe((数据)=>{..})