[英]Why GET request is not working in Node.js?
const express = require('express');
const mongoose = require ("mongoose");
const app = express();
const Student = require('../models/students');
require('dotenv').config();
const PORT = process.env.PORT || 3000;
const cors = require('cors');
app.use(express.json());
app.use(express.urlencoded({extended:true}));
app.use(cors());
app.use(express.static('./dist/mean-stack-application'));
mongoose.connect(process.env.MONGO_URL, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
}).then(() => {
console.log("Connection is Successful");
}).catch((e) => {
console.log("No Connection");
});
app.get("/studentData", async( req,res ) => {
try{
const getStudentsData = await Student.find();
console.log(getStudentsData);
res.send(getStudentsData);
}catch(e){
res.send(e);
}
});
app.listen(PORT, () => {
console.log(`Connection is setup at ${PORT}`);
});
Angular代码
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../environments/environment'
@Injectable({
providedIn: 'root'
})
export class RestoService {
constructor(private http: HttpClient) { }
readonly baseUrl = 'https://mean-curd-app.herokuapp.com';
readonly url = '/studentData';
getList(){
return this.http.get(`${this.baseUrl}${this.url}`)
}
}
它在 localhost 3000 上运行,但是当我在 Heroku 上托管这个 MEAN 堆栈应用程序时,我收到错误消息,即状态代码 304。连接成功但出现问题,这就是我收到此错误消息的原因。
GitHub Source Code Link: https://github.com/SwapnilVedpathak-sv/mean-stack-application Heroku Hosted Link: https://mean-curd-app.herokuapp.com
这里没有错。 304 不是错误。 这就是为提高效率而设置的 web 服务器的工作原理。 当一个合作的客户端请求他们之前在“条件 GET 请求”中请求的相同资源并且该资源自从他们之前缓存它以来没有改变时,然后服务器向他们发送一个 304。然后客户端可以使用他们的缓存版本.
有关详细信息,请参阅MDN 上的此说明。
HTTP 304 Not Modified 客户端重定向响应码表示不需要重传请求的资源。 它是对缓存资源的隐式重定向。 当请求方法是安全的(如 GET 或 HEAD 请求)或请求是有条件的并使用 If-None-Match 或 If-Modified-Since header 时,会发生这种情况。
您可能会在 Heroku 上看到它,因为这是您为生产部署配置的位置,因此启用了这些类型的优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.