简单的Express API错误

时间:2018-06-24 02:48:35

标签: javascript express

当我在下面运行我的express API时,我一直收到以下错误,该错误显示了我进入终端的错误,并将代码分成3个块,这些块将分为三张。如果您可以看看并让我知道。我还补充说,我进去了几次,重新整理/重新编码了作品。

*internal/modules/cjs/loader.js:573
throw err;
^
  Error: Cannot find module '/Users/eddie/Desktop/AllProjects/node- 
rest- 
  shop/node_modules/server.js'
at Function.Module._resolveFilename 
internal/modules/cjs/loader.js:571:15)
at Function.Module._load (internal/modules/cjs/loader.js:497:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
at startup (internal/bootstrap/node.js:228:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:576:3)

我已经重新安装了npm install express --save,我几次重写了我的代码,下面显示了代码

server.js代码

const http = require('http');
const app = require('./app')

const port = process.env.PORT || 3000

const server = http.createServer(app);


server.listen(port)

app.js代码

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


const productRoutes = require('./api/routes/products')
//middleware 
app.use('/products', productRoutes)

module.exports = app

products.js代码

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

router.get('/', (req, res, next) => {
  res.status(200).json({
    message: 'Handling get request to /products'
  })
})

router.post('/', (req, res, next) => {
  res.status(200).json({
    message: 'Handling post request to  /products'
  })
})


module.exports = router;

0 个答案:

没有答案