[英]Get method not able to be detected by the Node.js
我是node和express js的新手,今天我正在學習,並且我已經將node服務器初始化為:
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const PORT = 3000
const api=require('./routes/api')
const app = express()
app.use(bodyParser.json())
app.use(cors())
api.use('/api',api)
app.get('/', function(req, res) {
res.send('Hello from server')
})
app.listen(PORT, function(){
console.log("Server running on localhost:" + PORT)
});
我在服務器文件夾中創建了一個文件夾路由,並且有api.js文件,該文件具有GET方法來測試,api是否有效。在我擁有的api.js中,
const express = require('express')
const router=express.Router()
router.get('/', (req, res) => {
res.send('from Api Route');
})
module.exports=router
我輸入node server
,並顯示我:
Server running on localhost:3000
但是,當我嘗試獲取網址: http:// localhost:3000 / api時 ,它向我顯示:
並且,在箭頭函數sublime中的api.js文件中,向我顯示紅色標記錯誤為:
用api.use('/api',api)
替換app.use('/api',api)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.