express метод удаления работает на локальном хосте, но не один раз развернут на героку - PullRequest
0 голосов
/ 10 июля 2020

Я создал серверное приложение express, которое я тестировал на своем локальном хосте с расширением vscode клиента REST. Все методы GET, POST и DELETE работали отлично. Как только я портировал его на героку, метод DELETE перестал работать. Все остальные методы работают нормально, кроме удаления. Вот мой индекс. js код:

const express = require('express')
const app = express()
const cors=require('cors')

let notes = [
  {
    id: 1,
    content: "HTML is easy",
    date: "2019-05-30T17:30:31.098Z",
    important: true
  },
  {
    id: 2,
    content: "Browser can execute only Javascript",
    date: "2019-05-30T18:39:34.091Z",
    important: false
  },
  {
    id: 3,
    content: "GET and POST are the most important methods of HTTP protocol",
    date: "2019-05-30T19:20:14.298Z",
    important: true
  }
]
app.use(express.json())
app.use(cors())

app.get('/', (req,res) => {
    res.send('<h1>Hello World!</h1>')
})

app.get('/api/notes',(req,res) => {
    res.json(notes)
})
app.get('/api/notes/:id',(req,res) => {
    const id=req.params.id
    const note=notes.find(note=>note.id === parseInt(id))
    if(note){
        res.json(note)
    }
    else{
        res.status(404).end()
  }})
  
app.delete('/api/notes/:id', (request, response) => {    
  const id = Number(request.params.id)
  if(typeof(id)===number){  
    notes = notes.filter(note => note.id !== id)
    response.send(204).end()}
  else{
    response.status(404).end()
  }
})

const generateId = () => {
  const maxId = notes.length > 0
    ? Math.max(...notes.map(n => n.id))
    : 0
  return maxId + 1
}

app.post('/api/notes', (request, response) => {
  const body = request.body

  if (!body.content) {
    return response.status(400).json({ 
      error: 'content missing' 
    })
  }

  const note = {
    content: body.content,
    important: body.important || false,
    date: new Date(),
    id: generateId(),
  }

  notes = notes.concat(note)

  response.json(note)
})

const PORT = process.env.PORT || 3001
app.listen(PORT,() => {
    console.log(`Server running on port ${PORT}`)
})

Мой пакет. json файл:

{
  "name": "backend",
  "version": "0.0.1",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Kean Fernandes",
  "license": "MIT",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.17.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.4"
  }
}

Команды REST Client Раньше я делал запросы

POST https://intense-springs-95807.herokuapp.com/api/notes/
Content-Type: application/json

{
    "content":"true",
    "important":false
}

###
GET https://intense-springs-95807.herokuapp.com/api/notes/
###
DELETE https://intense-springs-95807.herokuapp.com/api/notes/4

Запросы POST и GET работают отлично, но команда DELETE не работает, хотя она возвращает успешный код 204 и не сообщает никакого содержимого.

HTTP/1.1 204 No Content
Server: Cowboy
Content-Length: 0
Connection: close
X-Powered-By: Express
Access-Control-Allow-Origin: *
Date: Fri, 10 Jul 2020 05:20:55 GMT
Via: 1.1 vegur

My heroku журналы:

2020-07-10T05:01:22.631314+00:00 heroku[router]: at=info method=DELETE path="/api/notes/4" host=intense-springs-95807.herokuapp.com request_id=509f7a4f-8d1c-4197-aa98-57cc52d44ffb fwd="106.51.242.88" dyno=web.1 connect=0ms service=5ms status=204 bytes=138 protocol=https                                                                                          2020-07-10T05:01:29.501906+00:00 heroku[router]: at=info method=GET path="/api/notes/" host=intense-springs-95807.herokuapp.com request_id=97505951-4b68-488d-97be-22693ba834c7 fwd="106.51.242.88" dyno=web.1 connect=0ms service=3ms status=304 bytes=183 protocol=https                                                                                              2020-07-10T05:01:36.719186+00:00 heroku[router]: at=info method=GET path="/api/notes/" host=intense-springs-95807.herokuapp.com request_id=51e9a0dc-4a45-4eb2-ada9-951ff57f2d62 fwd="106.51.242.88" dyno=web.1 connect=0ms service=4ms status=200 bytes=647 protocol=https                                                                                              2020-07-10T05:06:40.328192+00:00 heroku[router]: at=info method=GET path="/api/notes/" host=intense-springs-95807.herokuapp.com request_id=5400adff-97fc-4cff-9fb7-15e5e5ff515f fwd="106.51.242.88" dyno=web.1 connect=5ms service=5ms status=304 bytes=183 protocol=https                                                                                              2020-07-10T05:20:55.099827+00:00 heroku[router]: at=info method=DELETE path="/api/notes/4" host=intense-springs-95807.herokuapp.com request_id=42614282-168d-4cca-bbff-5dcc113e5125 fwd="106.51.242.88" dyno=web.1 connect=1ms service=3ms status=204 bytes=138 protocol=https         
...