forked from bgphipps/simplerestify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (30 loc) · 1.09 KB
/
server.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
var util = require('util'),
restify = require('restify'),
restifyValidator = require('restify-validator');
var server = restify.createServer({
name: 'simplerestify'
});
server.get(/.*/, restify.serveStatic({
'directory': 'src/html/',
'default': 'index.html'
}));
server.use(restify.bodyParser());
server.use(restify.queryParser());
server.use(restifyValidator);
server.post('/', function(req, res) {
req.assert('firstname', 'Invalid first name').isAlpha();
req.assert('lastname', 'Invalid last name').isAlpha();
req.assert('email', 'Invalid email address').isEmail();
req.assert('bday', 'Invalid birth day').isDate();
req.assert('username', 'Invalid user name').is(/.{6,}/);
req.assert('pass1', 'Invalid password').is(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).+$/).equals(req.params.pass2);
var errors = req.validationErrors();
if (errors) {
res.send(500, 'There have been validation errors: ' + util.inspect(errors));
return;
}
res.send(201);
});
server.listen(8000, function() {
console.log('%s listening at %s', server.name, server.url);
});