const express = require('express');
let router = express.Router();
router.get('/add-product',(req, res, next)=>{
res.send('<form action="/try" method="POST"><input type="text" name="title"><button type="submit">Sub,it</button> </form>');
});
package.json “依赖项”:{
"body-parser": "^1.19.0",
"express": "^4.17.1",
"funding": "^1.0.9"
它显示错误“无法解析的函数或方法get()” 我什至安装了express和body-parser
答案 0 :(得分:0)
您应该使用express
而不是express.Router()
let router = express();
答案 1 :(得分:0)
如果示例无法帮助您,请指定您的应用启动文件
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
const routerProducts = express.Router()
// middleware that is specific to this router
routerProducts.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now())
next()
})
// define the add-product route
routerProducts.post('/add-product', function (req, res) {
res.send('products create')
})
// define the get-product route
routerProducts.get('/get-product', function (req, res) {
res.send('products get')
})
app.use('/products', routerProducts )
app.listen(3000, () => console.log('server started'));