当我输入URL localhost:8000时,它将在浏览器“ hello world”中显示一条消息。 但是当我尝试localhost:8000 / product时,找不到正确的路径。
Server.js
const express = require('express');
const http = require('http');
const port = 8000;
const app = require('./app');
http.createServer(function(req,res){
res.write('Hello World!');
res.end(); }).listen(port);
app.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const productRoutes = require('./src/helper/routes/product');
console.log('savan');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/product',productRoutes);
app.get('/', (req,res) => {
if(req) {
console.log('request');
}
res.end();
});
src / helper / routes / product.js
const express = require('express');
const productRoutes = express.Router();
console.log('product');
module.exports = function(productRoutes) {
productRoutes.get('/', (req, res) => {
res.write('hii');
res.render('about');
console.log('get request...');
});
productRoutes.post('/',(req, res) => {
console.log('post request...');
});
return productRoutes;
};
答案 0 :(得分:1)
在路由文件中,您需要导出路由本身,而不是将所有内容包装在函数中。
void swap(int *i, int* j){
*i = *j;
*j = *i;
*i = *j;
}
void partition(int* array[], int size){
int pivot = size;
int i = - 1;
for(int j = 0 ; j < size - 1 ; j++){
if(array[j] < array[pivot]){
i++;
swap(array[i],array[j]);
}
}
}
int main() {
int array[] = {7,2,1,8,6,3,5,4};
int size = sizeof(array)/sizeof(array[0]);
partition(&array,size);
return 0;
}
或者您可以将该函数保留在路由文件中,然后调用该函数以将返回的Express路由器传递给app.js中的express应用,而不是直接传递该函数来表达。
const express = require('express');
const productRoutes = express.Router();
productRoutes.get('/', (req, res) => {
res.write('hii');
res.render('about');
console.log('get request...');
});
productRoutes.post('/',(req, res) => {
console.log('post request...');
});
module.exports = productRoutes;