I have a page with a sidebar that can change its function. Each different sidebar layout is a div. Two of the sidebars allow for text input. Right now I have one app.get statement that calls a handler that deals with all the sidebar layouts, but this solution is very messy. How would I go about splitting the different sidebars to work with different handler functions?
For example, I have this statement in app.js:
app.post('/', home.sidebarHandler);
sidebarHandler currently handles both logging in and registering as a new user, but I would like this to be done by two separate functions.
Here's part of my jade code, this is one of the divs:
form#sbCreateBox(method='post')
input.sbText(type='text', name= 'usernameReg', placeholder='Username', required)
input.sbText(type='text', name= 'acctHolderName', placeholder='Name')
input.sbText(type='email', name= 'useremail', placeholder='Email Address', required)
input.sbText(type='password', name= 'passwordReg', autocomplete='off', placeholder='Password', required)
input.sbText(type='password', name= 'passConfirm', autocomplete='off', placeholder='Confirm Password', required)
input#sbCreateButton(type='submit', value='Create Account')
How might I setup the routes to have separate app.post statements?
You could define two routes in your server side code:
app.post('/register', home.registerUser);
app.post('/login', home.loginUser);
And update your HTML to post to the correct URL (/register or /login) depending on the user's action.
EDIT You can create two forms. One will post to /register and the other to /login (notice the action attribute on the form element):
Register Form
form#sbCreateBox(method='post' action='/register')
input.sbText(type='text', name= 'usernameReg', placeholder='Username', required)
input.sbText(type='text', name= 'acctHolderName', placeholder='Name')
input.sbText(type='email', name= 'useremail', placeholder='Email Address', required)
input.sbText(type='password', name= 'passwordReg', autocomplete='off', placeholder='Password', required)
input.sbText(type='password', name= 'passConfirm', autocomplete='off', placeholder='Confirm Password', required)
input#sbCreateButton(type='submit', value='Create Account')
Login Form
form#sbCreateBox(method='post' action='/login')
input.sbText(type='text', name= 'usernameReg', placeholder='Username', required)
input.sbText(type='password', name= 'passwordReg', autocomplete='off', placeholder='Password', required)
input#sbCreateButton(type='submit', value='Login')