Comments (4)
the way I managed to make the socket work in conjunction with the overnight is as follows:
export class AppServer extends Server {
private readonly logger: Logger;
private close: http.Server;
constructor() {
super();
this.logger = new Logger();
this.logger.rmTimestamp = true;
this.app.use(cors());
this.app.use(compression());
this.app.use(helmet());
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({extended: true}));
this.app.use((req: Request, res: Response, next: NextFunction) => {
res.setHeader('Content-type', 'application/json');
res.setHeader('Accept', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
next();
});
this.mongoConnection();
this.setupControllers();
}
stop() {
this.close.close();
}
start() {
const port = 4000;
this.close = this.app.listen(port, () => {
this.logger.info('Server listening on port: ' + port);
});
}
initSocket() {
this.logger.info('initializing socket');
const io = require('socket.io')(this.close);
io.on('connection', (socket: Socket) => {
this.logger.info('user connected');
socket.on('chat', (data: any) => {
io.emit('chat', data);
});
});
}
private setupControllers() {
this.logger.rmTimestamp = true;
this.logger.info('\nRegistering controllers');
super.addControllers([
new ChatController(),
]);
}
private mongoConnection() {
const url = `${process.env.MONGODB_URL}:${process.env.MONGO_PORT}`;
connect(process.env.MONGODB_URI || url,
{
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
dbName: process.env.MONGO_DB_NAME,
user: process.env.MONGO_USER,
pass: process.env.MONGO_PASSWORD,
},
).catch((err) => {
throw err;
});
}
}
from overnight.
Haven't used socket.io so can't say. Just checked the docs here, https://socket.io/docs/#Using-with-Express. I don't see how OvernightJS would make any difference.
from overnight.
and my startup server
const server = new AppServer();
server.start();
server.initSocket();
from overnight.
Thanks for posting that. I'm sure some will find it useful.
from overnight.
Related Issues (20)
- 404 status on every route
- Overnightjs not generating sourcemap at all
- How to use JwtManager.middleware? HOT 3
- Question: TypeScript Configuration / getOwnMetadata() returns undefined HOT 7
- AddControllers not working HOT 9
- Unable to access "this" inside controllers HOT 5
- 'Server' no longer exported HOT 2
- Why not async-wrap everything by default? HOT 2
- IRouter error when build HOT 1
- JWT Middleware HOT 1
- 404 on all routes when using express-promise-router HOT 4
- Non-blocking file logging HOT 1
- Problem with @ClassErrorMiddleware HOT 2
- Help wanted: Setting up controllers: {ControllerName} HOT 3
- How to get type safe on req.params HOT 1
- overnight breaks when controllers and server are in separate projects.
- Not an issue: Thank you for this project HOT 1
- OvernightJS controllers cannot be extended
- Controller with multiple enpoint
- Is this repository deprecated? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from overnight.