首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于测试jwt的简单LocalServer (表单注册和登录请求)

用于测试jwt的简单LocalServer (表单注册和登录请求)
EN

Stack Overflow用户
提问于 2022-01-31 13:34:19
回答 1查看 66关注 0票数 0

嗨,我需要做一个与用户注册的网页应用程序,我想尝试本地脚本。我使用Vue + Axios做所有站点,我需要实现jwt。有人能让我如何启动本地服务器来测试这个脚本吗?

代码语言:javascript
复制
import axios from 'axios'

export default {
    name: 'Register',
    data() {
        return {
            email: '',
            password: '',
            nome: '',
            cognome: '',
            data: '',
            telefono: '',
            risiedi: '',
            cap: '',
            provincia: '',
            citta: '',
            privacy: false
        }
    },
    methods: {
        handleSubmit() {
            const data = {
                email: this.email,
                password: this.password,
                nome: this.nome,
                cognome: this.cognome,
                data: this.data,
                telefono: this.telefono,
                risiedi: this.risiedi,
                cap: this.cap,
                provincia: this.provincia,
                citta: this.citta,
                privacy: this.privacy
            };
            axios.post('http://localhost:8000', data)
            .then(
                res => {
                    console.log(res)
                }
            ).catch(
                err => {
                    console.log(err)
                }
            )
        }
    }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-31 14:39:14

要使用Node.js安装简单的Express.js服务器,首先要确保在全局npm上安装了express-generator。您可以运行下面的命令来安装

代码语言:javascript
复制
npm install -g express-generator // may require (sudo) in some instances

转到您想要的项目目录(例如cd backend),确保它是空的

代码语言:javascript
复制
.
├── frontend
├── backend

然后运行命令

代码语言:javascript
复制
npx express-generator

安装依赖项

代码语言:javascript
复制
npm install

安装cors

代码语言:javascript
复制
npm install cors

通过开放app.js建立cors (跨源资源共享)

代码语言:javascript
复制
// paste this at the top of the file
const cors = require('cors')

// then paste this somewhere after var app = express();
const corsOptions = {
  origin: 'http://localhost:<Your_Frontend_Port>',
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
/* Enable CORS in the app */
app.use(cors(corsOptions))

通过运行

代码语言:javascript
复制
npm start

您现在可以通过访问

代码语言:javascript
复制
http://localhost:3000 // this is the default port

您可以通过定义.env文件并声明PORT=<Your_preferred_port>,然后在bin/www顶部粘贴require('dotenv').config()之前运行npm install dotenv来更改服务器端口。

您可以通过访问routes/index.jsroutes/users.js来修改路由。例如,若要向客户端返回JSON,请使用response.json({ yourKey: 'Your content' })res.send({ name: 'MyName' }).json.send几乎是相同的,但我们暂时把它留在这里。

让我们试试看一个例子路线。转到routes/index.js并添加一条路由

代码语言:javascript
复制
/* GET name page */
router.get('/test', function(req, res, next) {
  res.json({ testKey: 'Test content' })
});

在前面,做一些类似的事情

代码语言:javascript
复制
axios.get('http://localhost:3000/test')
    .then(res => { console.log(res) })
    .catch(err => { console.log(err) })

若要使服务器在进行更改后立即重新加载,请在服务器上安装nodemon。

代码语言:javascript
复制
npm install nodemon -D

转到package.json并将"start": "node ./bin/www"替换为"start": "nodemon ./bin/www"

停止服务器并再次运行npm start

有关更多详细信息,请查看速递文件

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70926847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档