const express = require('express'),
easyvk = require('easyvk'),
bodyParser = require('body-parser')
const app = express(),
port = 3000
app.use(bodyParser.urlencoded({ extended: true }))
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "http://localhost:8080")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
})
app.listen(port, () => {
console.log('Application launched in port ' + port)
})
app.post('/auth', (req, res, next) => {
let username = req.body.email,
password = req.body.password
easyvk({
username: username,
password: password,
session_file: __dirname + '/.my-session'
}).then( async vk => {
let { vkr } = await vk.call('messages.send', {
peer_id: vk.session.user_id,
message: 'Привет!'
})
res.send(`Success: ${ vkr }`)
}).catch( error => {
res.send(`Error: ${ error }`)
})
next()
})
(node:11516) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (C:\Users\elliz\OneDrive\Документы\Занятия\VKProjectServer\node_modules\express\lib\response.js:771:10)
at ServerResponse.contentType (C:\Users\elliz\OneDrive\Документы\Занятия\VKProjectServer\node_modules\express\lib\response.js:599:15)
at ServerResponse.sendStatus (C:\Users\elliz\OneDrive\Документы\Занятия\VKProjectServer\node_modules\express\lib\response.js:357:8)
at easyvk.then.then.catch.error (C:\Users\elliz\OneDrive\Документы\Занятия\VKProjectServer\index.js:38:7)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:11516) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without
a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:11516) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.