let request = new XMLHttpRequest() request.open(`GET`, `/your-route?yourParameterName=yourValue`) request.send()
app.get(`/your-route`, (request, response) => { let yourValue = request.query.yourParameterName })
let session = require(`express-session`)
app.use(session({ secret: `your secret string`, resave: false, saveUninitialized: false, cookie: { // how long the session will last // put null to have session end when client closes browser maxAge: someMilliseconds } }))
request.session.yourVariable = something
request.session.yourVariable
request.session.destroy()
<input type="file">
FormData
object:
let formData = new FormData() formData.append(`yourKey`, yourFileInput.files[0]) let request = new XMLHttpRequest() request.open(`POST`, `/your-route`) request.send(formData)
let fileUpload = require(`express-fileupload`)
app.use(fileUpload())
let file = request.files.yourKey file.mv(`${__dirname}/your-folder/${file.name}`)
app.set(`view engine`, `ejs`)
views
, and move your HTML file to that folder.
.ejs
instead of .html
.
response.sendFile
with response.render
:
response.render(`index`) // if your HTML file is called index.ejs
let data = { yourKey: yourValue } response.render(`index`, data)
<%= yourKey %>
<% if (someCondition) { %> some HTML <% } %>
<%- include(`another-file.ejs`) %>
let ejsLayouts = require(`express-ejs-layouts`)
app.use(ejsLayouts)
views
folder,
add a new file called layout.ejs
, and add a template to
be used for every page. Put defineContent
where the
page-specific content should appear:
<html> <head> <title>Your Title</title> </head> <body> <h1>Your Heading</h1> <%- defineContent(`main`) %> </body> </html>
contentFor
at the
top of the file:
<%- contentFor(`main`) %>
module.exports.yourFunction = () => { // your code here }
index.js
, import the the other file:
let yourModule = require(`./other-file.js`)
index.js
, call the
function from the imported file:
yourModule.yourFunction()
let fs = require(`fs`)
let contents = fs.readFileSync(yourFile, `utf8`) // write code here to do something with the file contents // you can split the contents into separate lines with contents.split(/\r?\n/)
let axios = require(`axios`)
axios.post(someUrl)
let data = { yourKey: yourValue } axios.post(someUrl, data)
let data = { yourKey: yourValue } axios.post(someUrl, data).then(processResponse) function processResponse(response) { // write code here to do something with the response // you can get what was returned with response.data // you can get what was sent with response.config.data }