我有一个最小的nodejs服务器,可加载html页面。在该页面中,我有一个javascript函数,该函数在每次加载页面时都会调用。我的目标是每次加载页面时,前端都会向后端路由“ / getColors”发出请求,该路由将返回一个值(目前使用占位符字符串)。
我已设置好路由,一切都已设置并且正常工作,我的后端/ getColors路由被调用,我尝试使用Error: Could not find or load main class test
返回一个虚拟var,但是我无法在前端收到此字符串。
我错过了一些布线步骤吗?我正在使用快递和车把。我的server.js文件如下所示:
res.send('dummyVar')
它将所有路由发送到index.js:
//setup express app
const express = require('express');
const app = express();
var router = express.Router();
//port and hosting info
const port = 3000;
var host = '0.0.0.0';
//Loads the handlebars module
const handlebars = require('express-handlebars');
//Sets our app to use the handlebars engine
app.set('view engine', 'handlebars');
//Sets handlebars configurations (we will go through them later on)
app.engine('handlebars', handlebars({
layoutsDir: __dirname + '/views/layouts',
helpers: {
'compare': function (lvalue, operator, rvalue, options) {
var operators, result;
if (arguments.length < 3) {
throw new Error("Handlerbars Helper 'compare' needs 2 parameters");
}
if (options === undefined) {
options = rvalue;
rvalue = operator;
operator = "===";
}
operators = {
'==': function (l, r) { return l == r; },
'===': function (l, r) { return l === r; },
'!=': function (l, r) { return l != r; },
'!==': function (l, r) { return l !== r; },
'<': function (l, r) { return l < r; },
'>': function (l, r) { return l > r; },
'<=': function (l, r) { return l <= r; },
'>=': function (l, r) { return l >= r; },
'typeof': function (l, r) { return typeof l == r; }
};
if (!operators[operator]) {
throw new Error("Handlerbars Helper 'compare' doesn't know the operator " + operator);
}
result = operators[operator](lvalue, rvalue);
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
}
}));
//Tells app to use '/public' folder for static files
app.use(express.static('public'))
//connect all routes
const routes = require('./routes');
app.use('/', routes);
//use this folder for static files
app.use('/static/', express.static(__dirname + '/static/'));
//render and startup server
app.listen(port, () => console.log(`App listening to port ${port}`));
要加载的html页面具有以下标记:
const express = require('express');
const app = express();
var router = express.Router();
//tagger.site route
app.get('/tagger', function (req, res) {
res.render('newtagger', {
layout : 'newHomeindex',
pageTitle: 'tagger.site',
projectsTab:'active',
icon: 'https://cdn4.iconfinder.com/data/icons/48-bubbles/48/06.Tags-512.png'
});
})
app.post('/getColors', function(req, res){
//let filepath = req.body.filepath
console.log("/getColors route " )
//res.send('hello world')
res.send('value')
});
module.exports = app;
首次加载页面时,我可以看到<script type="text/javascript">
$(document).ready(function () {
getColors()
//make request to colors backend
function getColors(){
console.log("getColors()")
$.ajax({
type: 'POST',
url: '/getColors',
data: {
type: "varValue",
},
}).then(async function (data) {
console.log("REQUESTDONE data = ", data)
}).catch(function (err) {
console.log("REQUESTDONE err = ", err)
})
}
});
</script>
打印在chrome控制台中,因此我知道该函数已正确调用。在我的终端中,我可以看到getColors()
被打印出来,所以我知道后端函数正在被调用。但是我从来没有看到我的Chrome控制台中印有“ REQUESTDONE”字样。意味着永远不会收到该请求,是我错误地返回了带有res.send()的字符串,还是我在前端ajax请求中错误地捕获了该值?谢谢