app.post 与 socket.io 结果无法获取
app.post with socket.io results in cannot GET
我正在为我和我的朋友制作一个小应用程序,但由于某种原因无法使用。我是 socket.io 的新手,所以我无法自己弄明白...
所以我正在使用这个服务器端代码:
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post('/initiate', (request, response) => {
console.log("yey1");
const { body } = request;
initiate(body.command);
});
http.listen(3000, () => {
console.log('Listening on port 3000');
});
function initiate(command){
console.log("yey");
io.emit("command", command);
}
app.use(bodyParser.urlencoded({ extended: false }));
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', (req, res) => {
res.render(__dirname + '/public/index.html', {command: "Awaiting..."});
});```
And whenever you try to connect to <url>/initiate, it just says ```cannot GET /initiate```.
What am I doing wrong?
所以这里有很多问题我们需要解决,我会边做边解释。
/*
* So first thing you need to understand is Express or HTTP is a static server, so this
* means that once you start it or call the listen function, you cant really change
* anything after that, so you want to configure all your routes properly before calling
* the listen function.
*/
// use const because its not like your gonna reinstantiate these anytime soon
const bodyParser = require('body-parser');
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.use(bodyParser.json());
// This route has to be configured first because if you define it last, it will
// overwrite the initiate route that you have.
app.get('/', (req, res) => {
res.render(__dirname + '/public/index.html', {command: "Awaiting..."});
});
// The reason you're getting the GET error when browsing to it is because this route,
// has been configured to use the POST method so if you want it to be accessible on a
// GET method then use app.get
app.get('/initiate', (request, response) => {
console.log("yey1");
const { body } = request;
initiate(body.command);
});
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({ extended: false }));
// So you see, you have to setup the whole server first before starting it up
http.listen(3000, () => {
console.log('Listening on port 3000');
});
function initiate(command){
console.log("yey");
io.emit("command", command);
}
我希望这能帮助您克服学习中的一些障碍,祝您好运。
我正在为我和我的朋友制作一个小应用程序,但由于某种原因无法使用。我是 socket.io 的新手,所以我无法自己弄明白... 所以我正在使用这个服务器端代码:
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.post('/initiate', (request, response) => {
console.log("yey1");
const { body } = request;
initiate(body.command);
});
http.listen(3000, () => {
console.log('Listening on port 3000');
});
function initiate(command){
console.log("yey");
io.emit("command", command);
}
app.use(bodyParser.urlencoded({ extended: false }));
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get('/', (req, res) => {
res.render(__dirname + '/public/index.html', {command: "Awaiting..."});
});```
And whenever you try to connect to <url>/initiate, it just says ```cannot GET /initiate```.
What am I doing wrong?
所以这里有很多问题我们需要解决,我会边做边解释。
/*
* So first thing you need to understand is Express or HTTP is a static server, so this
* means that once you start it or call the listen function, you cant really change
* anything after that, so you want to configure all your routes properly before calling
* the listen function.
*/
// use const because its not like your gonna reinstantiate these anytime soon
const bodyParser = require('body-parser');
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.use(bodyParser.json());
// This route has to be configured first because if you define it last, it will
// overwrite the initiate route that you have.
app.get('/', (req, res) => {
res.render(__dirname + '/public/index.html', {command: "Awaiting..."});
});
// The reason you're getting the GET error when browsing to it is because this route,
// has been configured to use the POST method so if you want it to be accessible on a
// GET method then use app.get
app.get('/initiate', (request, response) => {
console.log("yey1");
const { body } = request;
initiate(body.command);
});
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.urlencoded({ extended: false }));
// So you see, you have to setup the whole server first before starting it up
http.listen(3000, () => {
console.log('Listening on port 3000');
});
function initiate(command){
console.log("yey");
io.emit("command", command);
}
我希望这能帮助您克服学习中的一些障碍,祝您好运。