I am not a very good programmer but i make an app with nodejs and express when i was making the app i use this line of code to host the server on localhost:8000 the line of code is :
var server = http.listen(8000,() => {
console.log("server is listening on port", server.address().port);
});
when i was using this line of code like that all was good.
When my app is done i changed the like of code to:
var server = http.listen(8000, "192.168.0.111", () => {
console.log("server is listening on port", server.address().port);
});
then only the html and css is working but not the server.js file, the server.js file is:
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var mongoose = require("mongoose");
const { sendStatus } = require("express/lib/response");
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
var dbUrl = "mongodb+srv://<username>:<password>@learning-node.14s4f.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
var Message = mongoose.model("Message", {
name: String,
message: String
});
app.get("/messages", (req, res) => {
Message.find({}, (err, messages) => {
res.send(messages);
});
});
app.post("/messages", (req, res) => {
var message = new Message(req.body);
message.save((err) => {
if (err) {
sendStatus(500);
}
Message.findOne({message: "badword"}, (err, censored) => {
if (censored) {
console.log("Censored word Found", censored);
Message.remove({_id: censored.id }, (err) => {
console.log("Removed censored Message");
})
}
});
io.emit("message", req.body);
res.sendStatus(200);
});
});
io.on("connection", (socket) => {
console.log("A new user Connected !");
});
mongoose.connect(dbUrl, (err) => {
console.log("MongoDB Connection", err);
});
var server = http.listen(8000, "192.168.0.111", () => {
console.log("server is listening on port", server.address().port);
});
and the mongoDB data is not being imported Please Help ME !!! T-T