Created
October 25, 2019 20:39
-
-
Save paultopia/b7e8f9f453c281f86ed8f11fffdd55de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tweetstorm(instring) { | |
var currentTweet = ""; | |
var tweets = []; | |
var words = instring.split(" "); | |
for (let item of words) { | |
if (currentTweet.length + item.length + 9 < 280) { | |
currentTweet = currentTweet + " " + item; | |
} else { | |
tweets.push(currentTweet); | |
currentTweet = item; | |
} | |
} | |
if (currentTweet.length > 0) { | |
tweets.push(currentTweet); | |
} | |
let numTweets = tweets.length; | |
var curNumber = 0; | |
var tweetCounter = ""; | |
for (let i = 0; i < numTweets; i++) { | |
curNumber = i + 1; | |
tweetCounter = ` (${curNumber}/${numTweets})`; | |
tweets[i] = tweets[i].trim() + tweetCounter; | |
} | |
let output = tweets.join("\n\n---\n\n"); | |
let page = `<html><body><pre>${output}</pre></body></html>`; | |
return page | |
} | |
const express = require("express"); | |
const app = express(); | |
var bodyParser = require("body-parser"); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: true})); | |
app.use(express.static("public")); | |
app.get("/", function(request, response) { | |
response.sendFile(__dirname + "/views/index.html"); | |
}); | |
app.post("/", function(req, res) { | |
if (!req.body.text) { | |
return res.send({ status: "error", message: "missing text" }); | |
} else { | |
return res.send(tweetstorm(req.body.text)); | |
} | |
}); | |
// listen for requests :) | |
const listener = app.listen(process.env.PORT, function() { | |
console.log("Your app is listening on port " + listener.address().port); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment