server.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const express = require("express");
  2. const bodyParser = require("body-parser");
  3. const fs = require("fs");
  4. const mongoose = require("mongoose");
  5. const sharp = require("sharp");
  6. const app = express();
  7. // const history = [];
  8. app.use(bodyParser.json());
  9. app.use(express.static("chat"));
  10. mongoose.connect("mongodb://localhost/chat", { useNewUrlParser: true });
  11. const db = mongoose.connection;
  12. const messageSchema = new mongoose.Schema({
  13. nick: String,
  14. message: String,
  15. image: String,
  16. replyTo: mongoose.Types.ObjectId,
  17. });
  18. const Message = mongoose.model("Message", messageSchema);
  19. const Image = mongoose.model("Image", messageSchema);
  20. db.on("error", console.error.bind(console, "connection error:"));
  21. //MESSAGE
  22. app.get("/message", async (req, res) => res.send(await Message.find()));
  23. app.get("/message/:id", async (req, res) => {
  24. res.send(
  25. await Message.findOne({ _id: mongoose.Types.ObjectId(req.params.id) })
  26. );
  27. });
  28. app.put("/message/:id", async (req, res) => {
  29. res.send(
  30. await Message.updateOne(
  31. { _id: mongoose.Types.ObjectId(req.params.id) },
  32. {
  33. nick: req.body.nick,
  34. message: req.body.message,
  35. }
  36. )
  37. );
  38. });
  39. app.delete("/message/:id", async (req, res) => {
  40. res.send(
  41. await Message.remove({ _id: mongoose.Types.ObjectId(req.params.id) })
  42. );
  43. });
  44. app.post("/message", async (req, res) => {
  45. let newMessage = new Message(req.body);
  46. await newMessage.save();
  47. res.status(201).send(newMessage);
  48. });
  49. //IMAGE
  50. app.get("/image", async (req, res) => res.send(await Image.find()));
  51. app.get("/image/:id", async (req, res) =>
  52. res.send(await Image.findOne({ _id: mongoose.Types.ObjectId(req.params.id) }))
  53. );
  54. app.delete("/image/:id", async (req, res) => {
  55. res.send(await Image.remove({ image: req.params.id }));
  56. });
  57. app.post("/image", async (req, res) => {
  58. let newImage = new Image(req.body);
  59. await newImage.save();
  60. res.status(201).send(newImage);
  61. });
  62. // app.get("/image", (req, res) => {
  63. // fs.readdirSync("chat/image").forEach((image) => {
  64. // console.log(image);
  65. // });
  66. // res.end(JSON.stringify(fs.readdirSync("chat/image")));
  67. // });
  68. // app.post("/image", (req, res) => {
  69. // let fileName = Math.random().toString("36");
  70. // fileName = `image/${fileName}`;
  71. // let fileStream = fs.createWriteStream("chat/" + fileName);
  72. // req.pipe(fileStream);
  73. // req.on("end", () => {
  74. // res.end(fileName);
  75. // });
  76. // });
  77. // app.get("/message", (req, res) => {
  78. // res.send(JSON.stringify(history));
  79. // });
  80. // app.get("/", (req, res) => {
  81. // res.send(JSON.stringify(history));
  82. // });
  83. // app.get("/message/:messageId", (req, res) => {
  84. // res.send(JSON.stringify(history[req.params.messageId]));
  85. // });
  86. // app.put("/message/:messageId", (req, res) => {
  87. // history[req.params.messageId] = req.body;
  88. // res.send(JSON.stringify(history[req.params.messageId]));
  89. // });
  90. // app.delete("/message/:messageId", (req, res) => {
  91. // history[req.params.messageId] = null;
  92. // res.send(JSON.stringify(history[req.params.messageId]));
  93. // });
  94. // app.post("/message", (req, res) => {
  95. // history.push(req.body);
  96. // console.log(req.body);
  97. // res.status(201).send(req.body);
  98. // });
  99. app.listen(5000, () => console.log("listen on 5000 port"));