'/'端点上的Express get请求无法正常工作,而'/ books'或'/ users'端点上的请求正常工作。我不知道我的代码有什么问题。由于我是Express框架的新手,因此我正在寻求帮助。下面是我的代码。谢谢。
// app.ts文件
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const usersRouter = require('./routes/users.js');
// All Routers
const bookRouter = require('./routes/bookRouter.ts');
const indexRouter = require('./routes/indexRouter.ts');
const config = require('./config.ts');
const PORT = 3000;
const app = express();
// adding express middleware
function isLoggedIn(req, res, next) {
if (!(req.session && req.session.user)) {
return res.send('Not logged in!');
}
next();
}
app.use(cors());
app.use(bodyParser.json());
// Establishing the connection
const url = config.mongoUrl;
const connect = mongoose.connect(url, { useNewUrlParser: true });
connect.then((db) => {
console.log('Connection Successful');
}, (err) => { console.log('Cannot connect to the database bookstore ', err);
});
app.use(express.static(path.join(__dirname, 'public')));
app.listen(PORT, () => {
console.log('Server started at port: ' + PORT);
});
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/books', bookRouter);
module.exports = app;
// indexRouter.ts文件
const express = require('express');
const router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
// res.render('index', { title: 'Express' });
console.log('Working');
res.send('get works');
});
module.exports = router;
// bookRouter.ts文件
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const express = require('express');
const bookRouter = express.Router();
const Books = require('../models/books.ts');
bookRouter.route('/')
.get((req, res, next) => {
Books.find(req.query)
.then((books) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
console.log('Books: ', books);
res.send('Works');
})
.catch(err => {
res.status(400).send('Unable to get data from database', err);
});
})
.post((req, res, next) => {
Books.create(req.body)
.then((dish) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(dish);
})
.catch(err => {
res.status(400).send('Unable to save to database');
});
})
.put((req, res, next) => {
res.statusCode = 403;
res.end('PUT operation not supported on /dishes');
})
.delete((req, res, next) => {
Books.deleteMany({})
.then((resp) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(resp);
});
});
bookRouter.route('/:bookId')
.get((req, res, next) => {
Books.findById(req.params.bookId)
.then((book) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(book);
});
})
.post((req, res, next) => {
res.statusCode = 403;
res.end('POST operation not supported on /books/' + req.params.bookId);
})
.put((req, res, next) => {
Books.findByIdAndUpdate(req.params.bookId, {
$set: req.body
}, {new: true})
.then((book) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(book);
});
})
.delete((req, res, next) => {
Books.findByIdAndRemove(req.params.bookId)
.then((resp) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.json(resp);
});
});
module.exports = bookRouter;