FarelDeveloper commited on
Commit
3c0a3bf
·
verified ·
1 Parent(s): 5812e92

Create server.js

Browse files
Files changed (1) hide show
  1. server.js +44 -0
server.js ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const express = require('express');
2
+ const path = require('path');
3
+ const TempMailCore = require('./tempmail');
4
+
5
+ const app = express();
6
+ const tm = new TempMailCore();
7
+
8
+ app.use(express.json());
9
+
10
+ // serve web
11
+ app.get('/', (_, res) => {
12
+ res.sendFile(path.join(__dirname, 'index.html'));
13
+ });
14
+
15
+ /*
16
+ CLONE API STYLE
17
+ contoh:
18
+ /api/email/new -> temp-mail /email/new
19
+ */
20
+
21
+ // list domain
22
+ app.get('/api/domains', async (_, res) => {
23
+ res.json(await tm.listDomains());
24
+ });
25
+
26
+ // create email
27
+ app.post('/api/email/new', async (req, res) => {
28
+ const { name, domain } = req.body;
29
+ if (!name || !domain)
30
+ return res.json({ success:false, error:'name & domain required' });
31
+
32
+ res.json(await tm.generateEmail(name, domain));
33
+ });
34
+
35
+ // inbox
36
+ app.get('/api/email/:email/messages', async (req, res) => {
37
+ res.json(await tm.checkEmail(req.params.email));
38
+ });
39
+
40
+ const PORT = process.env.PORT || 3000;
41
+ app.listen(PORT, () => {
42
+ console.log(`🚀 Temp Email Farel running`);
43
+ console.log(`🌐 http://localhost:${PORT}`);
44
+ });