repl.it
Nodejs

No description

fork
loading
Files
  • index.js
  • public
  • views
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');

require('dotenv').config();

const app = express();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.set('view engine', 'ejs');
app.use(express.static('public'));

app.use(session({
	secret: process.env.SESSION_SECRET || 'secret',
	name: 'session-id',
	resave: true,
	saveUninitialized: true,
	cookie: {
		httpOnly: true,
		secure: false
	}
}));

app.get('/', (req, res) => {
	res.render('home', Object.assign({x: Math.random()*10000, y: Math.random()*10000, c: 'aaa'}, players[req.sessionID]));
});

let players = {}

app.post('/location', (req, res) => {
	players[req.sessionID] = Object.assign({x: Number(req.body.x), y: Number(req.body.y), m: req.body.m, c: req.body.c}, players[req.sessionID]);
	players[req.sessionID].xp = (players[req.sessionID].xp || 0) + 1;
	res.json({all: players, you: req.sessionID});
});

app.listen(process.env.PORT);
node v10.16.0