This weconnect-server git repository contains the code for WeVote's Node Javascript application server.
- Node is the interpreter/compiler for server based JavaScript (JavaScript not running in a browser).
- The weconnect-server
- is based on the excellent Hackathon Starter "A kickstarter for Node.js Web applications". (But we use is as an API server).
- is a backend API server "written in Node" to support our React weconnect front end client.
- uses the Express.js application server to run our app, and serve up HTTP requests.
- uses the Prisma.js ORM (Object Relational Model) to read and write from our PostgreSQL database server.
Interested in volunteering or applying for an internship? Starting presentation here. Please also read about our values and see our Code of Conduct To join us, please review our openings here, and apply for a volunteer position through that page.
Our current version of our public facing web app is here https://WeVote.US and we are working on a new version now!
These instruction assume that you are installing on a Mac. If you use Windows or Linux, the installation procedure should be similar.
This procedure is based on using the free Community edition of WebStorm, which has great Git integraton, a great integrated Node debugger, and is an excellent editor. If you have the paid version of WebStorm the instructions should be the same. If you have some other preferred editor, we recommend that you still do this install, and then use your other editor as you wish!
If you don't already have one, create an account in GitHub
GitHub is where WeVote stores the source code for our various projects.
The free Community edition is at https://www.jetbrains.com/webstorm/
License it as a free Community installation.
Once installed, start WebStorm from Launch Pad or Spotlight
The first step is to press that "Clone Repository" button to clone the https://github.com/wevote/weconnect-server repository. Enter the URL and press the Clone button.
Now the latest code is on your machine.
If you like the default white characters on a black background, skip this step.
Access the settings dialog from the gear icon in the upper right hand side of the WebStorm app.
Set the Theme as you would like, or have it "Sync with OS" (which is my preference), and then save.
Plugins extend the capability of WebStorm and are worth exploring. If it sounds good, we usually install them, unless the suggestions are for off-topic for what we are useing WebStorm for today.
Plugins suggested by WebStorm are safe to install, and easy to remove if you don't like them.
Open a terminal window by clicking on the terminal icon on the bottom left side of WebStorm, type brew
and hit return, if brew is installed you can skip this step.
To Install Homebrew paste this following command into the terminal, and press return
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After a few minutes, homebrew will be installed. You will now use Homebrew to install some more applications.
This project needs a Node version of at least 22.0
If you have an earlier version of Node installed, you will need to reinstall it.
Check your node version via the terminal (This computer was at V18, and needed to be upgraded. Node had been previously installed with
Homebrew. "homebrew" is in the path to the Node executable (/opt/homebrew/bin/node
), so we know it was installed with Homebrew.)
stevepodell@Steves-MacBook-Air weconnect-server % which node
/opt/homebrew/bin/node
stevepodell@Steves-MacBook-Air weconnect-server % node -v
v18.10.0
stevepodell@Steves-MacBook-Air weconnect-server %
If your computer did not have Node installed with Homebrew, you will have to research how to upgrade your installation of Node.
If Node was installed with Homebrew or you have never installed Node, continue...
stevepodell@Steves-MacBook-Air weconnect-server % brew install node@22
...
==> Caveats
node@22 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have node@22 first in your PATH, run:
echo 'export PATH="/usr/local/opt/node@22/bin:$PATH"' >> ~/.zshrc
For compilers to find node@22 you may need to set:
export LDFLAGS="-L/usr/local/opt/node@22/lib"
export CPPFLAGS="-I/usr/local/opt/node@22/include"
==> Summary
πΊ /usr/local/Cellar/node@22/22.11.0: 2,628 files, 83.7MB
==> Running `brew cleanup node@22`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
stevepodell@Steves-MacBook-Air weconnect-server %
If Homebrew asks you to make the following 4 manual changes to link in Node. Execute these 4 lines in your terminal.
stevepodell@Steves-MacBook-Air weconnect-server % echo 'export PATH="/usr/local/opt/node@22/bin:$PATH"' >> ~/.zshrc
stevepodell@Steves-MacBook-Air weconnect-server % echo 'export PATH="/usr/local/opt/node@22/bin:$P
stevepodell@Steves-MacBook-Air weconnect-server % export LDFLAGS="-L/usr/local/opt/node@22/lib"
stevepodell@Steves-MacBook-Air weconnect-server % export CPPFLAGS="-I/usr/local/opt/node@22/include"
stevepodell@Steves-MacBook-Air weconnect-server %
Then confirm the version of Node is greater than V22, open a new terminal window (with the "+" icon) and type
stevepodell@Steves-MacBook-Air weconnect-server % node -v
v22.11.0
stevepodell@Steves-MacBook-Air weconnect-server %
At WeVote we use different naming conventions for origin
and upstream
than you might be familiar with from other projects, so you
will need to rename the default git origin (which at WeVote is your private branch in GitHub
)
Edit the origin line, and change the name to upstream, then press OK
Then press the + button and set up the new value for βoriginβ. (DONβT USE SailingSteve β use your GitHub handle β the GitHub username that is in the URL after you sign in to GitHub .)
When done, your remotes will look something like this (with your GitHub handle instead of SailingSteve!)
At this point you are poised to make Git branches and pull requests.
If you haven't already done this via a prompt from Webstorm, type
stevepodell@Steves-MacBook-Air weconnect-server % npm install
You can run this command as often as you want, and it will cause no harm.
Right-click on the .env-template
file in Webstorm, and paste it as .env
Open .env
in WebStorm by double-clicking on it
Modify the DATABASE_URL
line by substituting your Postgres username and password, and then save.
Here is a filled in example:
DATABASE_URL=postgresql://jerrygarcia:jerryspassword@localhost:5432/WeConnectDB?schema=public
Generate the schema from prisma/schema.prisma to node_modules
stevepodell@Steves-MacBook-Air weconnect-server % prisma generate
Prisma schema loaded from prisma/schema.prisma
β Generated Prisma Client (v5.22.0) to ./node_modules/@prisma/client in 73ms
Start by importing your Prisma Client (See: https://pris.ly/d/importing-client)
Help us improve the Prisma ORM for everyone. Share your feedback in a short 2-min survey: https://pris.ly/orm/survey/release-5-22
stevepodell@Steves-MacBook-Air weconnect-server %
Initialize the generated schema into the postgres database server.
stevepodell@Steves-MacBook-Air weconnect-server % prisma migrate dev --name init
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "WeConnectDB", schema "public" at "localhost:5432"
Applying migration `20241126190549_init`
The following migration(s) have been created and applied from new schema changes:
migrations/
ββ 20241126190549_init/
ββ migration.sql
Your database is now in sync with your schema.
β Generated Prisma Client (v5.22.0) to ./node_modules/@prisma/client in 104ms
stevepodell@Steves-MacBook-Air weconnect-server %
Open the pull-down that initially says "Current File", and select Edit Configurations
In the Run/Debug Configurations dialog, press the "+" button and then select "Node.js"
Then fill in the run configuration...
- Enter
Start weconnect-server
in the Name field - Enter
weconnect-server.js
in the File field. - And press "OK" to save
Next create a run config to start postgres
- Add another "New Configuration", this time for a Shell Script (close to the bottom of the list of configurations on the right)
- In this "Run/Debug Configurations" dialog, add a name "Start Postgres"
- In the "Script Path" paste in
/usr/local/opt/postgresql@14/bin/postgres
in the "Script Options" paste in-D /usr/local/var/postgresql@14
- Remove any text in the "Interpreter Path" field.
- Make sure "Execute in the terminal" is checked
- Then press OK to save
(This run configuration is the equivalent of typing
/usr/local/opt/postgresql@14/bin/postgres -D /usr/local/var/postgresql@14
in the terminal.)
stevepodell@Steves-MacBook-Air weconnect-server % brew install --cask pgadmin4
This will take a few minutes, when it completes launch the app from Launch Pad or Spotlight
Register the server as WeVoteServer
And in the Connection tab set the Host name as localhost β also add your postgres Username and Password, then save
First start postgres via the run configuration
Then start weconnect-server.js with the run configuration.
As you can see when you press the Green start arrow, the server starts up in a terminal window where you can see
logging. Any console.log()
lines that you put in the code will appear in this JavaScript console for this Node based server (which has no DOM).
Alternatively if you press the green bug icon, you start a debugging session, where you can set breakpoints, examine threads, and examine data in a familiar way to what you might be used to with Chrome Dev Tools (Inspect) or debugging in PyCharm.
When you navigate in Chrome to http://localhost:4000/
you will see the client view of app (in these early days the UI is
generated on the server via the Pug UI package), this UI of course is rendered as a DOM within Chrome,
and as with any web app, right-clicking on the page and choosing Inspect, will allow you to run the Chrome Dev Tools.
Mad respect to Hackathon Starter
The MIT License (MIT)
Copyright (c) 2024 We Vote USA Forked from Hackathon Starter Copyright (c) 2024 Sahat Yalkabov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.