const express = require('express')
const app = express()
var ipgeoblock = require("node-ipgeoblock")
var server=app.listen(3000, function () {
var host=server.address().address;
var port=server.address().port
console.log('Example app listening on port 3000!',host,port)
});
app.use(ipgeoblock({
geolite2: "./GeoLite2-Country.mmdb",
blockedCountries: [ "FR", "GB", "IN"]
}));
app.get('/', function (req, res) {
res.send('Hello World!')
})
what is wrong in this code, is there any localhost problem ,why am i not able to block countries
Related
I'm using socket.io in my express application.
I have one route and view that uses socket.io on the client-side browser.
Sooner or later the socket.io code will get larger and I would like to modularize it.
This is what I have so far and it works just fine but I am wondering what is the conventional way to modularize socket.io in an express.js app?
server.js
const app = require('./app');
const PORT = process.env.PORT || 3000;
app.listen(PORT, (req, res) => {
console.log(`Listening on port: ${PORT}`);
});
app.js (took out unnecessary things)
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const { socketIo } = require('./utilities/socket.io');
const roomRoutes = require('./routes/rooms');
/* Middleware */
app.get('/', (req, res) => {
res.render('index');
});
// Routes
app.use('/users', userRoutes);
app.use('/rooms', roomRoutes);
socketIo(io);
module.exports = http;
./utilities/socket.io
module.exports.socketIo = async (io) => {
io.on('connection', (socket) => {
socket.on('join-room', (roomId) => {
socket.join(roomId);
socket.on('chat-message', msg => {
io.to(roomId).emit('chat-message', msg);
});
});
});
}
I write this code and req.body is undefined
I want to get post value in my program
can you help me, please?
const express = require('express')
const app = express()
const port = 3000
const crypto = require('crypto');
function sha1(s) {
return crypto.createHash("sha1")
.update(s)
.digest("hex");
}
app.post("/flag", (req, res) => {
console.log(req.body);
});
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
You body-parser npm package
$ npm i body-parser
var express = require('express')
var bodyParser = require('body-parser')
const app = express()
const port = 3000
const crypto = require('crypto');
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
function sha1(s) {
return crypto.createHash("sha1")
.update(s)
.digest("hex");
}
app.post("/flag", (req, res) => {
console.log(req.body);
});
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Instead of using body parser, Express already providing support for that,
import express from "express";
const app = express();
app.use(express.json());
app.use(express.urlencoded());
This should be given before the Api route
I'm having trouble understanding how web sockets work. I'm not seeing my console.log "connection" inside the io.on function when I visit http://www.localhost:3010 in my browser. Could someone explain why that is?
server.js:
const app = require("./backend/app")
const http = require('http');
const server = http.createServer(app)
var socketIo = require('socket.io')
server.listen(3010, () => {
console.log("listening")
})
const io = socketIo(server)
io.on("connection", (socket) => {
console.log("connection")
socket.emit("news", {hello: "world"})
})
app.js:
const express = require("express");
const app = express();
app.get("/", (req, res, next) => {
res.send("hello world");
})
module.exports = app
I'm beginner to Node, I just need to create a express server on a button click event, I did as below but it shouldn't work
script.js
$('#button').click(function(){
$.post('/test');
});
app.js
app.post('/test', function (req, res) {
var express = require('express');
var app = express();
app.get('/test', function (req, res) {
res.send('Hello World');
})
var server = app.listen(3100, function () {
var host = server.address().address
var port = server.address().port
})
});
Following is my code as follows:
var express = require('express');
var app = express();
var router = express.Router();
app.route('/')
.get(function(req, res) {
res.send('Hello World!');
});
app.route('/user')
.get(function (req, res) {
res.send('Hello' + req.params.id);
});
var server = app.listen(8000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
It runs fine with http://localhost:8000/ but with http://localhost:8000/user?id=D it gives following error: Cannot GET /user?id=D.
WHat is wrong in my code? Please help.
Thanks.
This route syntax:
'/user/:id'
matches a URL like this:
http://localhost:8000/user/4095
If you use a URL like this:
http://localhost:8000/user?id=D
then, you will need to use a "/user" route and read the query parameter for the id value from req.query.id as described here.
In addition, your don't need the app.route() as it's just an extra level of complication for things you are not doing here. I'd suggest this simplification which I have tested and works:
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.get('/user', function (req, res) {
res.send('Hello: ' + req.query.id);
});
var server = app.listen(8000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});